【问题标题】:Getting Error 403 in Scrapy while crawling Cloudflare protected website在抓取受 Cloudflare 保护的网站时在 Scrapy 中出现错误 403
【发布时间】:2021-01-19 08:25:57
【问题描述】:

我的目标是从this website 抓取产品页面上的评论。我在 Python 3.7.1 中使用 Scrapy。

由于这是一个受 cloudflare 保护的网站,我设置了用户代理:'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43' 、设置ROBOTSTXT_OBEY = True DOWNLOAD_DELAY = 5

第一个请求我仍然得到 403,scrapy 收到的响应是 hCaptcha 页面,希望我证明有人正在访问该页面。

我尝试了 cfscrape 模块,但问题仍然存在。

以下是cmd输出:

2021-01-19 13:52:03 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2021-01-19 13:52:04 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.g2.com/robots.txt> (referer: None)
2021-01-19 13:52:04 [protego] DEBUG: Rule at line 1 without any user agent to enforce it on.
2021-01-19 13:52:04 [protego] DEBUG: Rule at line 2 without any user agent to enforce it on.
2021-01-19 13:52:09 [scrapy.core.engine] DEBUG: Crawled (403) <GET https://www.g2.com/products/solidworks-product-designer/reviews/> (referer: None)
2021-01-19 13:52:09 [scrapy.core.engine] INFO: Closing spider (finished)

Here 是由 scrapy 看到的带有响应输出的 codepen。

Edit1:为响应文本添加 Codepen 链接

【问题讨论】:

  • Clouldflare 保护是为了阻止机器人/脚本而创建的,它专门发送 403 来阻止像你这样的脚本。我不知道是否有完全可行的方法来解决验证码。它需要Machine Learning(或Neural NetworkComputer Vision)来识别图像——这是一个复杂的问题。有些门户网站提供了解决验证码的方法,但您必须为每个已解决的验证码付费。他们中的一些人只是付钱给其他人来解决您的验证码。

标签: python web-scraping scrapy web-crawler http-status-code-403


【解决方案1】:

这是一个有趣的案例。如果我从开发检查器将浏览器请求复制为 CURL,它确实会通过 cloudflare 触发 403。但是,如果我使用ScrapeNinja 模拟浏览器,并添加“Referer”标头,它就会通过,这是工作代码(node.js,如果您在将其转换为 Python 时遇到问题,请告诉我):

const fetch = require('node-fetch');

let req = fetch('https://scrapeninja.p.rapidapi.com/scrape', {
  method: 'POST',
  headers: 
    {
    "Content-Type": "application/json",
    "x-rapidapi-host": "scrapeninja.p.rapidapi.com",
    "x-rapidapi-key": "YOUR-RAPIDAPI-KEY"
    },
  body: JSON.stringify(       {
    "url": "https://www.g2.com/products/solidworks-product-designer/reviews",
    "headers": [
        "authority: www.g2.com",
        "referer: https://www.g2.com/",
        "pragma: no-cache",
        "cache-control: no-cache",
        "sec-ch-ua: \" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"97\", \"Chromium\";v=\"97\"",
        "sec-ch-ua-mobile: ?0",
        "sec-ch-ua-platform: \"macOS\"",
        "upgrade-insecure-requests: 1",
        "user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36",
        "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "sec-fetch-site: none",
        "sec-fetch-mode: navigate",
        "sec-fetch-user: ?1",
        "sec-fetch-dest: document",
        "accept-language: en-US,en;q=0.9"
       ]
       })

    });
req.then((res) => res.json()).then(json => console.log(json))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多