我正在尝试使用来自PyCharm的请求函数。
import requests
url = 'https://www.google.com'
ProxyDict = {"http_proxy":"http://proxy-us.MyCompany.com:911","https_proxy":"http://proxy-us.MyCompany.com:912"}
o = requests.get(url,proxies = ProxyDict)
print(o.status_code)它给了我
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000027CBF508B00>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))但当我试图从cmd跟踪时,它起了作用。
$set http_proxy=http://proxy-us.MyCompany.com:911
$set https_proxy=http://proxy-us.MyCompany.com:912
$ python ip_locator.py
200我试过以下几点:
在Pycharm, --> HTTP_PROXY ->自动代理配置URL:http://proxy-us.MyCompany.com:911中名为HTTP_PROXY和HTTPS_PROXY
但是,我的任何方法都没有成功地在PyCharm中设置代理。有什么建议吗?
发布于 2020-11-12 01:13:11
看来我找到了解决办法。
Pycharm ->运行->编辑配置->环境变量
PYTHONUNBUFFERED=1;http_proxy=http://proxy-us.MyCompany.com:911;https_proxy=http://proxy-us.MyCompany.com:912 发布于 2020-11-12 01:18:50
您将错误的字典键传递给requests.get。
根据docs,密钥应该命名为http和https:https://requests.readthedocs.io/en/master/user/advanced/#proxies
文档中的示例:
proxies = {
'http': 'http://10.10.1.10:3128',
'https': 'http://10.10.1.10:1080',
}
requests.get('http://example.org', proxies=proxies)但是,由于代理也可以通过环境变量进行配置,所以第二种方法是可行的:您正在设置http_proxy和https_proxy环境变量。requests接受它们作为代理配置。
https://stackoverflow.com/questions/64796432
复制相似问题