问题描述
解决办法
方法一,按照提示添加对应的前缀
通过排查发现了原因,就是在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'}
方法二,更新需要的requests包
pip3 install --upgrade requests
方法三,重置网络代理
unset http_proxy
unset https_proxy
方法四,指定网络代理
set HTTPS_PROXY=http://username:password@proxy.example.com:8080
set https_proxy=http://username:password@proxy.example.com:8080
Where
proxy.example.com
is the proxy address (in my case it islocalhost
) and 8080 is my port.You can figure out your username by typing
echo %username%
in your command line. As for the proxy server, on Windows, you need to go to "Internet Options" -> "Connections" -> LAN Settings and tick "Use a proxy server for your LAN". There, you can find your proxy address and port.
完结撒花
每一只自由的灵魂都应该被尊重