Proxyhandler处理器

Proxyhandler处理器(代理):
1.代理的原理:在请求目的网站之前,先请求代理服务器,然后让代理服务器去请求目的网站,代理服务器拿到目的网站的数据后,再转发给我们的代码。
2.http://httpbin.org:这个网站可以方便的查看http请求的一些参数。
3.在代码中使用代理
    *使用url1ib. request. Proxyhandler,传入一个代理,这个代理是一个字典,字典的key依赖于代理服务器能够接收的类型,一般是http或者https值是ip:port
    *使用上一步创建的 handler,以及 request. build_ opener创建一个opener对象。
    *使用上一步创建的 opener,调用`open函数,发起请求。
示例代码如下
python

url=http://httpbin.org/ip
#1使用 Proxyhandler,传入代理构建一个 handler
handler-request.Proxyhandler({"http":"XXX.XXX.XXX.XX:8010"})
#2.使用上面创的 handler建个 opener
opener -request. build opener(handler)
#3.使用opener去发送一个请求
resp= opener.open(url)
print(resp.read())

猜你喜欢

转载自blog.csdn.net/q947448283/article/details/85233307