Python异步爬虫requests和aiohttp中 * 的使用
作者:Dream丶Killer 时间:2021-06-26 11:56:05
爬虫要想爬的好,IP代理少不了。。现在网站基本都有些反爬措施,访问速度稍微快点,就会发现IP被封,不然就是提交验证。下面就两种常用的模块来讲一下 * 的使用方式。话不多说,直接开始。
requests中 * 的使用:
requests
中使用 * 只需要添加一个proxies
参数即可。proxies
的参数值是一个字典,key
是代理协议(http/https),value
就是ip和端口号,具体格式如下。
try:
response = requests.get('https://httpbin.org/ip', headers=headers,
proxies={'https':'https://221.122.91.74:9401'}, timeout=6)
print('success')
# 检测 * 是否使用成功
# 第一种方式,返回发送请求的IP地址,使用时要在 get() 添加 stream = True
# print(response.raw._connection.sock.getpeername()[0])
# 第二种方式,直接返回测试网站的响应数据的内容
print(response.text)
except Exception as e:
print('error',e)
注意: peoxies
的key
值(http/https
)要和url
一致,不然会直接使用本机IP直接访问。
aiohttp中 * 的使用:
由于requests
模块不支持异步,迫不得已使用aiohttp
,掉了不少坑。
它的使用方式和requests
相似,也是在get()
方法中添加一个参数,但此时的参数名为proxy
,参数值是字符串,且字符串中的代理协议,只支持http
,写成https
会报错。
这里记录一下我的纠错历程。。
首先根据网上的使用方式,我先试了一下下面的代码。
async def func():
async with aiohttp.ClientSession() as session:
try:
async with session.get("https://httpbin.org/ip", headers=headers,
proxy='http://183.220.145.3:80', timeout=6) as response:
page_text = await response.text()
print('success')
print(page_text)
except Exception as e:
print(e)
print('error')
if __name__=='__main__':
asyncio.run(func())
修改后,再来
async def func():
con = aiohttp.TCPConnector(verify_ssl=False)
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
try:
async with session.get("https://httpbin.org/ip", headers=headers,
proxy='http://183.220.145.3:80', timeout=6) as response:
# print(response.raw._connection.sock.getpeername()[0])
page_text = await response.text()
print(page_text)
print('success')
except Exception as e:
print(e)
print('error')
非但没有解决反倒多了一个警告,好在改一下就好。额~懒得粘了,直接来最终版本吧。。
# 修改事件循环的策略,不能放在协程函数内部,这条语句要先执行
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
async def func():
# 添加trust_env=True
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False), trust_env=True) as session:
try:
async with session.get("https://httpbin.org/ip", headers=headers,
proxy='http://183.220.145.3:80', timeout=10) as response:
page_text = await response.text()
print(page_text)
print('success')
except Exception as e:
print(e)
print('error')
虽然纠错过程有点长,但好在知道怎么用了。
来源:https://blog.csdn.net/qq_43965708/article/details/109622238
标签:Python,requests,aiohttp,代理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
开展全面的网站评估
2007-09-27 19:21:00
Python3 使用pip安装git并获取Yahoo金融数据的操作
2023-11-17 21:56:48
![](https://img.aspxhome.com/file/2023/1/63941_0s.jpg)
用户体验之网页板块设计
2011-05-14 16:41:00
阿里云ECS服务器部署django的方法
2023-04-09 10:00:26
浅析Python 抽象工厂模式的优缺点
2021-08-12 01:33:17
![](https://img.aspxhome.com/file/2023/1/90431_0s.png)
PHP脚本监控Nginx 502错误并自动重启php-fpm
2023-09-26 00:11:45
详解Golang语言HTTP客户端实践
2023-09-17 13:52:07
PHP安全的URL字符串base64编码和解码
2023-09-06 22:04:45
asp根据出生时间判断生肖
2008-03-24 19:49:00
python多线程中的定时器你了解吗
2021-09-01 23:39:21
Python找出文件中使用率最高的汉字实例详解
2021-11-06 06:37:53
Pytorch maxpool的ceil_mode用法
2023-03-20 13:28:05
详解Python的Flask框架中生成SECRET_KEY密钥的方法
2023-03-07 13:46:07
aspJpeg图片水印有杂点的完美解决方法
2011-02-05 10:55:00
python基础之迭代器与生成器
2022-02-20 07:07:54
Python+Qt身体特征识别人数统计源码窗体程序(使用步骤)
2021-06-03 10:40:54
![](https://img.aspxhome.com/file/2023/5/66745_0s.jpg)
DW中如何使用Library
2007-02-03 11:39:00
如何提高网页的维护效率
2009-03-01 11:33:00
使用WingPro 7 设置Python路径的方法
2022-05-23 03:59:56
![](https://img.aspxhome.com/file/2023/5/69005_0s.jpg)
HTML文件HEAD内部标签用法浅析
2008-07-06 20:56:00