python爬虫07--代理

"""
使用代理,突破目标网站访问限制
"""
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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值