python代理访问

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值