【问题标题】:Web Scraping - Cloudflare Issues网页抓取 - Cloudflare 问题
【发布时间】:2021-12-27 02:49:03
【问题描述】:

我正在尝试抓取 https://www.carsireland.ie/search#q?%20scraper%20python=&toggle%5Bpoa%5D=false&page=1(我已经构建了一个抓取工具,但后来他们对他们的网站进行了全面检修)。新网站采用了新格式,并使用 Cloudflare 提供通常的安全性。我有以下代码返回 403 错误,特别是引用此错误:

  • “https://www.cloudflare.com/5xx-error-landing”

目前我构建的代码如下:

from requests_html import HTMLSession

session = HTMLSession()

header = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" ,
    'referer':'https://www.google.com/'
}

# url of search page
url = 'https://www.carsireland.ie/search#q?sortBy=vehicles_prod%2Fsort%2Fpoa%3Aasc%2Cupdated%3Adesc&page=1'

# create a session with the url
r = session.get(url, headers=header)

# render the url
data = r.html.render(sleep=1, timeout=20)

# Check the response
print(r.text)

我真的很愿意提供任何帮助来纠正我遇到的 CloudFlare 问题。

【问题讨论】:

  • 403 对应于Forbidden 状态。您能否详细说明您如何知道这不是目标站点的 Cloudflare 配置的故意行为,以防止此类自动抓取...?

标签: python web-scraping python-requests python-requests-html


【解决方案1】:

只需将 header 中的 referer 属性更改为您要抓取的链接即可解决此问题。

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 2021-07-30
    • 2021-07-25
    • 1970-01-01
    • 2016-02-05
    • 2015-06-26
    • 2023-03-04
    相关资源
    最近更新 更多