#导入需要的库
from urllib.request import Request,build_opener
from fake_useragent import UserAgent
from urllib.request import ProxyHandler
#创建url
url = "http://httpbin.org/get"
#创建随机表头
headers = {
"User-Agent" : UserAgent().chrome
}
#响应
request = Request(url,headers=headers)
#设置代理,如果有账号密码 用第一个
#handler = ProxyHandler({"http": "398707160:j8inhg2g@120.27.224.41:16818"})
headler = ProxyHandler({"http" : "113.194.28.196:9999"})
opener = build_opener(headler)
#响应
resp = opener.open(request)
#输出
print(resp.read().decode())
总结: opener还不知道怎么意思,代理ip不好找.
网友评论