urllib中的本地代理设置

python3中的urllib设置本地代理:

import urllib.request
import urllib.parse

# 113.79.75.104:9797
# 创建handler
handler = urllib.request.ProxyHandler({'http': '114.215.95.188:3128'})
# 常见opener
opener = urllib.request.build_opener(handler)

url = 'http://www.baidu.com/s?ie=UTF-8&wd=ip'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
}
request = urllib.request.Request(url, headers=headers)

response = opener.open(request)

with open('ip.html', 'wb') as fp:
    fp.write(response.read())

 

posted on 2020-03-08 10:40  共感的艺术  阅读(304)  评论(0编辑  收藏  举报