[python]代码库
# encoding:utf-8
import urllib2
import random
# 读取IP地址
iplist = ['121.15.183.100:8888', '117.127.0.100:80', '122.114.31.100:80']
# 随机选择一个IP地址
proxy = random.choice(iplist)
# 构建一个Handler处理器对象,参数是一个字典类型,包括代理类型和IP
httpproxy_handler = urllib2.ProxyHandler({"http": proxy})
# 构建一个没有代理的处理器对象
nullproxy_handler = urllib2.ProxyHandler({})
# 构建一个opener
proxySwitch = True # 定义一个代理开关
if proxySwitch:
opener = urllib2.build_opener(httpproxy_handler)
else:
opener = urllib2.build_opener(nullproxy_handler)
# 构建了一个全局的opener,之后所有的请求都可以用urlopen()方式去发送,也附带handler一些参数
urllib2.install_opener(opener)
# 使用带有代理的urlopen打开网页
response = urllib2.urlopen("https://yuncode.net/")
html = response.read().decode("utf-8")
print(html)