1 #!/usr/local/bin/python3.7 2 3 import urllib.request 4 import urllib.parse 5 6 # 创建handler 7 handler = urllib.request.ProxyHandler({'http':'114.215.95.188:3128'}) 8 # 通过handler创建opener 9 opener = urllib.request.build_opener(handler) 10 # 请求URL 11 url = 'https://www.baidu.com/s?wd=ip' 12 # 设置请求头 13 headers = { 14 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15' 15 } 16 # 创建请求 17 request = urllib.request.Request(url,headers=headers) 18 #发送请求 19 response = opener.open(request) 20 21 # 保存返回的信息 22 with open('/Users/mozili/PYTHONWORKSPACE/Reptile/ip.html','wb') as fp: 23 fp.write(response.read())