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())