ProxyHandler处理器(代理):
代理的原理:在请求目的网站之前,先请求代理服务器,然后让代理服务器去请求目的网站,代理服务器拿到目的网站的数据后,再转发给我们的代码。
http://httpbin.org:这个网站可以方便的查看http请求的一些参数。
在代码中使用代理:
使用
urllib.request.ProxyHandler
,传入一个代理,这个代理是一个字典,字典的key依赖于代理服务器能够接收的类型,一般是http
或者https
,值是ip:port
。使用上一步创建的
handler
,以及request.build_opener
创建一个opener
对象。使用上一步创建的
opener
,调用open
函数,发起请求。
示例代码如下:
from urllib import request
url = 'http://httpbin.org/ip'
# 1. 使用ProxyHandler,传入代理构建一个handler
handler = request.ProxyHandler({"http":"223.241.78.43:8010"})
# 2. 使用上面创建的handler构建一个opener
opener = request.build_opener(handler)
# 3. 使用opener去发送一个请求
resp = opener.open(url)
print(resp.read())