如何修复在Python中使用requests库时出现的“ProxyError: 无法连接到代理,隧道连接失败: 403 Forbidden”?

0 投票
1 回答
225 浏览
提问于 2025-04-14 18:13

我正在做一个自己的小项目,选择了www.pythonanywhere.com来托管它。

为了实现一些功能,我需要从网站上获取信息。

但是当我运行下面的代码时,

import requests


response = requests.get(
        "https://example.com", 
        proxies={"http":"http://162.120.71.11:80"}, 
        headers={"User-Agent": "Opera/9.80 (X11; Linux x86_64; U; de) Presto/2.2.15 Version/10.00"}
        )

print( response.text )

我遇到了一个错误。

raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='example.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', O
SError('Tunnel connection failed: 403 Forbidden')))

我尝试更改用户代理和代理服务器,但没有任何变化。

我还尝试向不同的网站发送请求,有些网站可以正常工作,但我需要的网站却不行。

1 个回答

0

我也遇到过同样的问题,看来如果你使用的是免费计划,域名需要在白名单里:

https://www.pythonanywhere.com/forums/topic/27261/

撰写回答