爬虫代理ip设置(ip代理 爬虫)

作者:小哥哥
围观群众:45
更新于

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

90503dd800af882964c05583130f36a5.jpg

一、单个ip设置代理

import urllib.request

def 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.request

def 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()

非特殊说明,本文版权归 17天经验网 所有,转载请注明出处.

本文分类: 娱乐

本文标题: 爬虫代理ip设置(ip代理 爬虫)

本文网址: http://17tyw.com/yule/1492.html

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。