为request添加一个代理,及将浏览器头部信息加入,随机从ip列表中拿出一个ip进行访问
注意函数参数的形式,如request.proxyhandler(协议,地址)
import urllib.request
import random
url='http://www.baidu.com'
iplist=['121.226.174.246:8080','210.38.1.142:8080','210.38.1.143:8080']
proxyhandler=urllib.request.ProxyHandler({'http':random.choice(iplist)})
openner=urllib.request.build_opener(proxyhandler)
openner.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0')]
urllib.request.install_opener(openner) response=urllib.request.urlopen(url)
html=response.read().decode('utf-8') print(html)