900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > python爬虫request设置代理IP原理

python爬虫request设置代理IP原理

时间:2023-04-12 20:53:34

相关推荐

python爬虫request设置代理IP原理

在我们书写爬虫程序的时候,可能都多多少少会遇到ip被封的问题,或者说ip被限制的问题,那么就需要用到ip代理了,那么ip代理在request里面的原理究竟是怎么实现的呢?下面来了解一下。

单个ip设置代理

import urllib.requestdef create_handler():url = '/ip'# 添加代理proxy = {# 'http': 'http://119.5.72.6:4226' # 这是官方定义的'http': '119.5.72.6:4226'}# 代理处理器proxy_handler = urllib.request.ProxyHandler(proxy)# 创建自己的openeropener = 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)# 创建openeropener = urllib.request.build_opener(proxy_head)try:print(opener.open('/ip', timeout=1).read())print('=='*20)except Exception as e:print(e)if __name__ == '__main__':proxy_handel()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。