python使用urllib.request模块中的ProxyHandler可进行代理访问网页
def proxyOpenUrl(url):
timeOut = 3 # 设定重连次数
for out in range(1,timeOut+1):
print('[INFO]:第%d次尝试连接' % out)
ipList = openFile('proxy_ip.txt') # 代理ip文件
ip = random.choice(ipList)
print('代理IP为:%s' % ip)
try:
proxy_handler = urllib.request.ProxyHandler({'http':ip})
opener = urllib.request.build_opener(proxy_handler)
opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; \
WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36')]
urllib.request.install_opener(opener) # 安装代理后,后面的urlopen全部使用代理访问
response = urllib.request.urlopen(url)
return response # 返回response
except urllib.error.URLError:
print('网络出现异常!尝试更换代理IP')
continue
except ConnectionResetError as error:
print(str(error))
continue
print('[INFO]:连接错误!')
return False
测试网站:
http://www.whatismyip.com.tw