【问题标题】:Http - Tunnel connection failed: 403 Forbidden error with Python web scrapingHttp - 隧道连接失败:Python Web 抓取出现 403 禁止错误
【发布时间】:2020-02-18 18:32:28
【问题描述】:

我正在尝试抓取一个 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')

谁能帮我解决这个问题。提前致谢

【问题讨论】:

  • 你多久尝试一次抓取它?
  • 你是怎么解决的?

标签: python web-scraping http-error


【解决方案1】:

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

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

requests.get(url, proxies=proxyDict)

您可以找到更多信息here

【讨论】:

  • 我尝试添加代理,它没有显示任何错误。但是当我尝试提取文本时,它显示网页被阻止。*** 网页被阻止 ***
【解决方案2】:

我尝试使用 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”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2021-10-10
    • 2022-11-10
    相关资源
    最近更新 更多