首页 > 技术文章 > 3 ProxyHandle实现代理ip

sruzzg 2020-06-08 09:18 原文

快代理: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'{\n  "origin": "本机IP地址"\n}\n'
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'{\n  "origin": "139.196.52.1"\n}\n'
20 print(resp.read())

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

 

推荐阅读