(异步爬虫)requests和aiohttp中代理IP的使用

(异步爬虫)requests和aiohttp中代理IP的使用


爬虫要想爬的好,IP代理少不了。。现在网站基本都有些反爬措施,访问速度稍微快点,就会发现IP被封,不然就是提交验证。下面就两种常用的模块来讲一下代理IP的使用方式。话不多说,直接开始。

在这里插入图片描述
requests中代理IP的使用:
requests中使用代理IP只需要添加一个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是否使用成功
    # 第一种方式,返回发送请求的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会报错。
这里记录一下我的纠错历程。。
首先根据网上的使用方式,我先试了一下下面的代码。

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')

在这里插入图片描述
虽然纠错过程有点长,但好在知道怎么用了。

结尾塞张美图,撒由那拉~
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43965708/article/details/109622238