python出現Cannot connect to proxy錯誤 - 四十九C-137
原文地址:https://joessem.com/archives/python-connot-connect-to-proxy.html
在做 requests.get () 請求時出現 Cannot connect to proxy 的錯誤。
錯誤信息為:
Traceback (most recent call last):
?File "C:\Python38\lib\site-packages\requests\adapters.py", line 489, in send
? ?resp = conn.urlopen(
?File "C:\Python38\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
? ?retries = retries.increment(
?File "C:\Python38\lib\site-packages\urllib3\util\retry.py", line 592, in increment
? ?raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='cn.bing.com', port=443): Max retries exceeded with url: /HPImageArchive.aspx?format=js&idx=0&n=1 (Caused by ProxyError('Cannot connect to proxy.', FileNotFoundError(2, 'No such file or directory')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
?File "f:/Project/api/main.py", line 45, in <module>
? ?get_bing_daily_img()
?File "f:/Project/api/main.py", line 39, in get_bing_daily_img
? ?api_rets = requests.get(api_url, verify=False)
?File "C:\Python38\lib\site-packages\requests\api.py", line 73, in get ? ?return request("get", url, params=params, **kwargs)
?File "C:\Python38\lib\site-packages\requests\api.py", line 59, in request ? ?return session.request(method=method, url=url, **kwargs)
?File "C:\Python38\lib\site-packages\requests\sessions.py", line 587, in request
? ?resp = self.send(prep, **send_kwargs)
?File "C:\Python38\lib\site-packages\requests\sessions.py", line 701, in send
?File "C:\Python38\lib\site-packages\requests\adapters.py", line 559, in send
? ?raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='cn.bing.com', port=443): Max retries exceeded with url:
解決方案:
在進行 requests.get (url) 方法調用前,請設置目標 url 的代理過濾, 例如:
os.environ['NO_PROXY'] = "cn.bing.com"?
api_rets = requests.get(api_url, verify=False)
? ? ? ? ? ? ? ? ? ?
本文使用 文章同步助手 同步