爬虫request如何设置代理IP?在我们书写爬虫程序的时候,可能都多多少少会遇到ip被封的问题,或者说ip被限制的问题,那么就需要用到ip代理了,那么ip代理在request里面的原理究竟是怎么实现的呢?下面和天启HTTP来了解一下。一、单个ip设置代理import urllib.requestdef create_handler(): url = ‘http://httpbin.org/ip' # 添加代理 proxy = { # ’http‘: ’http://119.5.72.6:4226' # 这是官方定义的 ‘http’: ‘119.5.72.6:4226’ } # 代理处理器 proxy_handler = urllib.request.ProxyHandler(proxy) # 创建自己的opener opener = urllib.request.build_opener(proxy_handler) # 拿着代理ip去发送请求 data = opener.open(url).read() print(data)if name == ‘main’: create_handler()二、多个ip设置代理import urllib.requestdef proxy_handel(): proxy_list = [ {‘http’: ‘125.111.149.163:4205’}, {‘http’: ‘106.46.136.93:4225’}, {‘http’: ‘114.230.18.38:4228’}, {‘http’: ‘115.151.50.141:4273’}, {‘http’: ‘182.105.201.153:4275’}, ] for proxy in proxy_list: print(proxy) # 创造处理器 proxy_head = urllib.request.ProxyHandler(proxy) # 创建opener opener = urllib.request.build_opener(proxy_head)try: print(opener.open(‘http://httpbin.org/ip', timeout=1).read()) print(’==‘*20) except Exception as e: print(e)if name == ’main‘: proxy_handel()
爬虫request如何设置代理IP?
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。