温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python使用IP代理示例及出错解决方法

发布时间:2020-07-18 05:50:07 来源:网络 阅读:7517 作者:老鹰a 栏目:安全技术


一、代码示例

# -*- coding:utf-8 -*-

import requests

header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
}

proxy_ip = {
    'https': 'https://115.28.148.137:8118'
}
html = requests.get("http://www.baidu.com", headers=header, proxies=proxy_ip)
print html
print html.text

二、通过修改proxy_ip参数的代理IP服务器地址,可以更改代理IP


三、报错提示

requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', error(10054, '')))


出错的原因一般有2个:

1、ip代理服务器不能正常使用

2、IP代理使用的协议不正常,例如http://和https://是不能写错的


如果是http代理服器的话,则如下写:

proxy_ip = {
    'http': 'http://115.28.148.137:8118'
}

如果是https代理服务器的话,则如下写法:

proxy_ip = {
    'https': 'https://115.28.148.137:8118'
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI