"""
使用代理,突破目标网站访问限制
"""
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)
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐