(Python)python3.7以后requests模块proxy(代理)失效问题解决方案ProxySchemeUnknown: Not supported proxy scheme None

16 篇文章 0 订阅
12 篇文章 1 订阅

最近在使用requests模块写爬虫的时候,使用到了代理服务proxy,出现了不支持代理方案的错误,即如下的报错:ProxySchemeUnknown Traceback (most recent call last) ProxySchemeUnknown: Not supported proxy scheme None。

原因:

通过排查发现了原因,就是在Python3.6以后,在使用代理时,requests.get(url=url, headers=headers, proxies=…)中proxies的参数值发生了变化,3.6包括之前,proxies={‘https’: ‘127.0.0.1:8080’}或者proxies={‘http’: ‘127.0.0.1:8080’}即可,但是这样的字典类型并不适用于Python3.7及以上的版本。在Python3.7及以上版本,必须要在ip:port前面加上http://或者https://,绝对不能去掉前面的http://或者https://,即Python3.7后必须使用proxies={‘http’: ‘http://127.0.0.1:8080’}或者proxies={‘https’: ‘https://127.0.0.1:8080’}

#Python3.6 不需要加http://
proxy_pool = {
	'http': '127.0.0.1:8080',
	'https': '127.0.0.1:8080',
}
response = requests.get(url=url, headers=headers, proxies=random.choice(proxy_pool)) #proxies={'http': '127.0.0.1:8080'}

#Python3.7及以上 必须加上http://,不加就会报错
proxy_pool = {
	'http': 'http://127.0.0.1:8080',
	'https': 'https://127.0.0.1:8080',
}
response = requests.get(url=url, headers=headers, proxies=random.choice(proxy_pool)) #proxies={'http': 'http://127.0.0.1:8080'}
总结:

在Python3.7及以上版本中基于requests模块使用代理,传给proxies的参数值必须加上http://或者https://,不加就会报错,proxies的参数值为{'http': 'http://ip:port'}键值对类型的字典
`

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值