爬虫要想爬的好,IP代理少不了。。现在网站基本都有些反爬措施,访问速度稍微快点,就会发现IP被封,不然就是提交验证。下面就两种常用的模块来讲一下代理IP的使用方式。话不多说,直接开始。
requests中代理IP的使用:
requests
中使用代理IP只需要添加一个proxies
参数即可。proxies
的参数值是一个字典,key
是代理协议(http/https),value
就是ip和端口号,具体格式如下。
1
2
3
4
5
6
7
8
9
10
11
|
try : response = requests.get( 'https://httpbin.org/ip' , headers = headers, proxies = { 'https' : 'https://221.122.91.74:9401' }, timeout = 6 ) print ( 'success' ) # 检测代理IP是否使用成功 # 第一种方式,返回发送请求的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中代理IP的使用:
由于requests
模块不支持异步,迫不得已使用aiohttp
,掉了不少坑。
它的使用方式和requests
相似,也是在get()
方法中添加一个参数,但此时的参数名为proxy
,参数值是字符串,且字符串中的代理协议,只支持http
,写成https
会报错。
这里记录一下我的纠错历程。。
首先根据网上的使用方式,我先试了一下下面的代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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()) |
修改后,再来
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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' ) |
非但没有解决反倒多了一个警告,好在改一下就好。额~懒得粘了,直接来最终版本吧。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 修改事件循环的策略,不能放在协程函数内部,这条语句要先执行 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' ) |
虽然纠错过程有点长,但好在知道怎么用了。
到此这篇关于Python异步爬虫requests和aiohttp中代理IP的使用的文章就介绍到这了,更多相关requests和aiohttp中代理IP内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_43965708/article/details/109622238