MENU

python出现Cannot connect to proxy错误

November 18, 2022 • 阅读: 2806 • 笔记&折腾

在做 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: /HPImageArchive.aspx?format=js&idx=0&n=1 (Caused by ProxyError('Cannot connect to proxy.', FileNotFoundError(2, 'No such file or directory')))

解决方案:

在进行 requests.get(url) 方法调用前,请设置目标 url 的代理过滤, 例如:

os.environ['NO_PROXY'] = "cn.bing.com"
api_rets = requests.get(api_url, verify=False)
Leave a Comment