标签:open span http 设置 import pen read print urlopen
1 from urllib import request
# 1 使用Proxyhandler 传入构建handler
2 handler=request.ProxyHandler({‘http‘:‘10.0.0.1:8080‘})
# 2 使用创建的handler 构建一个opener
3 opener=request.build_opener(handler)
4 rep=request.Request(‘http://www.baidu.com‘)
# 3 使用opener 去发送
5 resp=opener.open(rep)
6 print(resp.read().decode(‘utf-8‘))
from urllib import request
url="http://www.httpbin.org/ip"
resp=request.urlopen(url)
with open("1.txt",‘w‘,encoding=‘utf-8‘)as pf:
pf.write(resp.read().decode(‘utf-8‘))
1.txt
{"origin":"124.88.160.199"}
标签:open span http 设置 import pen read print urlopen
原文地址:https://www.cnblogs.com/klmyoil/p/9116115.html