Http-隧道连接失败:Python Web抓取的403禁止的错误

时间:2020-02-18 18:32:28

标签: python web-scraping http-error

我正在尝试抓取http网站的网站,并且在尝试阅读该网站时遇到错误。

HTTPSConnectionPool(host='proxyvipecc.nb.xxxx.com', port=83): Max retries exceeded with url: http://campanulaceae.myspecies.info/ (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden',)))

以下是我在类似网站上编写的代码。我尝试使用urllib和user-agent仍然是同样的问题。

url = "http://campanulaceae.myspecies.info/"

response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'})
soup = BeautifulSoup(response.text, 'html.parser')

任何人都可以帮助我解决该问题。预先感谢

2 个答案:

答案 0 :(得分:0)

您应该在请求网址时尝试添加代理。

proxyDict = { 
          'http'  : "add http proxy", 
          'https' : "add https proxy"
        }

requests.get(url, proxies=proxyDict)

您可以找到更多信息here

答案 1 :(得分:0)

我尝试使用 User-Agent: Defined 并且对我有用。

url = "http://campanulaceae.myspecies.info/"
headers = {
"Accept-Language" : "en-US,en;q=0.5",
"User-Agent": "Defined",
}
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.text
soup = BeautifulSoup(data, 'html.parser')
print(soup.prettify())

如果您收到错误消息“bs4.FeatureNotFound:找不到具有您请求的功能的树构建器:html-parser。”那么这意味着您没有使用正确的解析器,您需要在顶部导入 lxml 并安装模块,然后在制作汤时使用“lxml”而不是“html.parser”。