""" 使用代理,突破目标网站访问限制 """ import urllib.request import random url = 'http://www.whatismyip.com.tw/' #代理IP地址列表 iplist = ['114.222.128.63:808','121.232.147.94:9000','117.90.2.112:9000','61.134.25.106:3128','183.158.202.248:9000'] # proxy_suppert = urllib.request.ProxyHandler({"http":random.choice(iplist)}) # 定制openner,给代理可以添加头等 opener = urllib.request.build_opener(proxy_suppert) # 给代理添加请求头 opener.add_handler = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0')] # 安装一个opener urllib.request.install_opener(opener) # 请求目标网址 response = urllib.request.urlopen(url) #print(response.read()) #读取请求资源并解码为utf-8 html = response.read().decode("utf-8") print(html)
python爬虫07--代理
最新推荐文章于 2024-08-06 11:55:39 发布