WordPress课程广告

Python requests 异常Proxy URL had no scheme, should start with http:// or https://解决

释放双眼,带上耳机,听听看~!

异常原因

Python requests 异常Proxy URL had no scheme, should start with http:// or https://解决,在使用Python requests的proxy代理功能的时候出现了这个错误,意思是我们使用的代理方案应该以http:// or https://开头。

pycharm运行Python requests代码出现requests.exceptions.InvalidURL: Proxy URL had no scheme, should start with http:// or https://异常的截图

网上看到的原因是说在Python3.7及以上版本中使用requests模块的代理功能,proxies的参数值必须加上http://或者https://(proxy = {‘http’: ‘http://ip:port’}这个键值对格式的字典),不加就会报错。不过其文章中贴出的错误类型是:

ProxySchemeUnknown Traceback (most recent call last) ProxySchemeUnknown: Not supported proxy scheme None,如果你出现了这个错误,应该一样可以使用下面的方式解决(我所使用的Python版本是3.9)。

至于是否真的是Python版本的原因,我没有去深究。

解决办法

给我们使用的代理加上http://或者是https://协议头,如下示例代码:

示例代码

# 报错代码
# proxy = {'http': '127.0.0.1:8888', 'https': '127.0.0.1:8888'}
r = requests.get(url, proxies=proxy)

解决方式一

proxy = {'http': 'http://127.0.0.1:8888', 'https': 'https://127.0.0.1:8888'}
r = requests.get(url, proxies=proxy)

解决方式二

proxy = {'HTTP': '127.0.0.1:8888', 'HTTPS': '127.0.0.1:8888'}
r = requests.get(url, proxies=proxy)

只需要把ip和端口换成你自己实际使用的即可。

网上还看见过key使用的是http而出现其他报错的(即方式一),如果你还是遇见了错误,可以尝试使用方式二将key大写的方式,使用方式二value中的http:// or https:// 加不加都可以,都不会出现Proxy URL had no scheme, should start with http:// or https://的报错。

给TA打赏
共{{data.count}}人
人已打赏
PycharmPython笔记

Pycharm项目目录文件夹图标小圆点代表什么意思

2024-5-25 16:30:56

Python内置函数Python列表Python笔记

Python列表合并的6种方法

2024-8-19 18:28:06

WordPress网站建设宣传图片
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
展开目录