【问题标题】:python requests problem: cloudflare error message "enable cookies"python请求问题:cloudflare错误消息“启用cookies”
【发布时间】:2020-07-03 06:16:35
【问题描述】:

我正计划为 Sneakersnstuff.com 网站创建一个基本的网络爬虫,但由于一个错误,我的努力提前停止了。当请求 url https://www.sneakersnstuff.com/ 时,而不是显示网站的 html,甚至是入口验证码,我被重定向到带有错误消息“启用 cookie”的 cloudflare 页面。我的代码和响应都如下所示

import requests
import cfscrape


session = requests.session()

response = session.get('https://www.sneakersnstuff.com/')

print(response.headers)
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en-US">
<!--<![endif]-->

<head>
    <title>Access denied | www.sneakersnstuff.com used Cloudflare to restrict access</title>
    <meta charset="UTF-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <meta name="robots" content="noindex, nofollow" />
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
    <link rel="stylesheet" id="cf_styles-css" href="/cdn-cgi/styles/cf.errors.css" type="text/css"
        media="screen,projection" />
    <!--[if lt IE 9]><link rel="stylesheet" id='cf_styles-ie-css' href="/cdn-cgi/styles/cf.errors.ie.css" type="text/css" media="screen,projection" /><![endif]-->
    <style type="text/css">
        body {
            margin: 0;
            padding: 0
        }
    </style>


    <!--[if gte IE 10]><!-->
    <script type="text/javascript" src="/cdn-cgi/scripts/zepto.min.js"></script>
    <!--<![endif]-->
    <!--[if gte IE 10]><!-->
    <script type="text/javascript" src="/cdn-cgi/scripts/cf.common.js"></script>
    <!--<![endif]-->



</head>

<body>
    <div id="cf-wrapper">
        <div class="cf-alert cf-alert-error cf-cookie-error" id="cookie-alert" data-translate="enable_cookies">Please
            enable cookies.</div>
        <div id="cf-error-details" class="cf-error-details-wrapper">
            <div class="cf-wrapper cf-header cf-error-overview">
                <h1>
                    <span class="cf-error-type" data-translate="error">Error</span>
                    <span class="cf-error-code">1020</span>
                    <small class="heading-ray-id">Ray ID: 578133293d83e0d6 &bull; 2020-03-22 16:13:25 UTC</small>
                </h1>
                <h2 class="cf-subheadline">Access denied</h2>
            </div><!-- /.header -->

            <section></section><!-- spacer -->

            <div class="cf-section cf-wrapper">
                <div class="cf-columns two">
                    <div class="cf-column">
                        <h2 data-translate="what_happened">What happened?</h2>
                        <p>This website is using a security service to protect itself from online attacks.</p>

                    </div>



                </div>
            </div><!-- /.section -->

            <div class="cf-error-footer cf-wrapper">
                <p>
                    <span class="cf-footer-item">Cloudflare Ray ID: <strong>578133293d83e0d6</strong></span>
                    <span class="cf-footer-separator">&bull;</span>
                    <span class="cf-footer-item"><span>Your IP</span>: 96.241.108.243</span>
                    <span class="cf-footer-separator">&bull;</span>
                    <span class="cf-footer-item"><span>Performance &amp; security by</span> <a
                        href="https://www.cloudflare.com/5xx-error-landing?utm_source=error_footer" id="brand_link"
                        target="_blank">Cloudflare</a></span>

                </p>
            </div><!-- /.error-footer -->


        </div><!-- /#cf-error-details -->
    </div><!-- /#cf-wrapper -->

    <script type="text/javascript">
        window._cf_translation = {};


    </script>

</body>

</html>

我曾尝试使用许多名为 cfscrape 的库推荐,但无济于事。

【问题讨论】:

  • 因“投票违规”而被暂停

标签: python web request python-requests cloudflare


【解决方案1】:

使用请求时,我通过在受支持的标头中提供用户代理为我解决了这个问题。在我使用导致问题的用户代理之前。现在我将其更改为 Mozilla (Sending "User-agent" using Requests library in Python),它可以工作。

不幸的是,响应消息并不能真正帮助找出问题所在。

【讨论】:

    【解决方案2】:

    在抓取受 CloudFlare 保护的网站时,您需要执行以下操作:

    1. 确保您发送的标头与浏览器发送的标头相同(并且顺序相同)
    2. 确保您使用的是非数据中心 IP 地址范围

    如果它仍然不起作用,就像我的情况一样......

    我在抓取一个电子商务网站(guess dot com)时遇到了同样的问题。显然,CloudFlare 会分析请求的 TLS 指纹并抛出 403 (1020) 代码,以防指纹与通常用于抓取的 node.js/python/curl 匹配。解决方案是模拟一些流行浏览器的指纹——最明显的方法是使用 Puppeteer.js 和 puppeteer 额外的隐身插件。但是..由于 Puppeteer 对我的用例来说不够快(我说得委婉些.. Puppeteer 在资源和迟缓方面很疯狂)我不得不构建一个使用无聊SSL(Chrome 使用的 SSL 库)的实用程序 - 因为编译 C/C++ 代码并找出某些 TLS 库的隐蔽编译错误对于大多数 Web 开发人员来说并不有趣 - 我将其包装为 API 服务器,您可以在这里尝试:https://rapidapi.com/restyler/api/scrapeninja

    详细了解 CloudFlare 如何分析 TLS:https://blog.cloudflare.com/monsters-in-the-middleboxes/

    【讨论】:

      【解决方案3】:

      Browser/User-Agent Filtering 添加到 cloudcraper 对我有用。

      import cloudscraper
      from bs4 import BeautifulSoup
      
      # Adding Browser / User-Agent Filtering should help ie. 
      
      # will give you only desktop firefox User-Agents on Windows
      scraper = cloudscraper.create_scraper(browser={'browser': 'firefox','platform': 'windows','mobile': False})
      
      html = scraper.get("https://www.sneakersnstuff.com/").content
      
      soup = BeautifulSoup(html, 'html.parser')
      
      print(soup)
      

      【讨论】:

        【解决方案4】:
        import cloudscraper
        from bs4 import BeautifulSoup
        
        scraper = cloudscraper.create_scraper()
        
        html = scraper.get("https://www.sneakersnstuff.com/").content
        
        soup = BeautifulSoup(html, 'html.parser')
        
        print(soup)
        

        输出:

        cloudscraper.exceptions.CloudflareReCaptchaProvider: Cloudflare reCaptcha detected, unfortunately you haven't loaded an anti reCaptcha provider correctly via the 'recaptcha' parameter.
        

        下一步?

        第 3 方 reCaptcha 解算器 说明

        cloudscraper 目前支持以下 3rd 方 reCaptcha 求解器,如果您需要的话。

        anticaptcha
        deathbycaptcha
        2captcha
        9kw
        return_response
        

        【讨论】:

        • 尝试此操作时,我一直收到错误,cloudscraper.exceptions.CloudflareCode1020: Cloudflare has blocked this request (Code 1020 Detected)。
        • @ChrisYun 确实由于多次请求,您的设备被阻止了。
        • 如何解决?
        • @ChrisYun 您可以将requests 库与proxiessocks 一起使用
        • 有没有办法在不使用代理的情况下在本地执行此操作?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 2020-12-13
        • 1970-01-01
        • 2019-04-13
        • 2013-07-21
        • 1970-01-01
        相关资源
        最近更新 更多