3 ProxyHandle实现代理ip

快代理:https://www.kuaidaili.com/ops/

西刺免费代理:http://www.xicidaili.com/

代理云:http://www.dailiyun.com/

 1 """ProxyHandler实现代理ip"""
 2 
 3 
 4 import urllib.request
 5 
 6 # # 没有使用代理
 7 # url= 'http://httpbin.org/ip'
 8 # resp = request.urlopen(url)
 9 # print(resp.read()) # b'{
  "origin": "本机IP地址"
}
'
10 
11 # 使用代理
12 url= "http://httpbin.org/ip"
13 # 1使用ProxyHandler传入代理构建一个handler
14 handler = urllib.request.ProxyHandler({"http": "139.196.52.1:8080"})
15 # 2使用上面创建的handler构建一个opener
16 opener = urllib.request.build_opener(handler)
17 # 3使用opener发送一个请求
18 resp = opener.open(url)
19 # b'{
  "origin": "139.196.52.1"
}
'
20 print(resp.read())

注意用免费代理ip运行是不稳定的

原文地址:https://www.cnblogs.com/sruzzg/p/13063762.html