【问题标题】:Python and CloudFlare issuePython 和 CloudFlare 问题
【发布时间】:2022-11-24 03:54:31
【问题描述】:

我碰壁试图提出要求https://1stkissmanga.io/由于 CloudFlare 保护。我准备了标头和 cookie(我从 Firefox 读取)但仍然没有成功。奇怪的是,我可以使用 wget 正确获取此站点。这是我不明白的问题——wget 没有任何 CloudFlare 绕过机制,所以如果它在 wget 中工作,那么它不应该在 Python 请求中工作吗? 当然,对于 wget,我仍然需要提供 cookie 值,否则 wget 也会攻击 CloudFlare。 使用 wget(成功结果):

wget "https://1stkissmanga.io/" -U "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0" --header="Cookie: __cf_bm=<some long string with dots and other special characters>"

使用蟒蛇:

headers = {"user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0",} cookies = {"__cf_bm": "<some long string with dots and other special characters>",}

url = "https://1stkissmanga.io/" res = requests.get(url, headers=headers, cookies=cookies)

我也尝试将 cookie 放入标头中

headers = {"user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0", "cookie": "__cf_bm=<some long string with dots and other special characters>",}

并执行res = requests.get(url, headers=headers),但结果是一样的。无论我做什么,请求始终停止 CloudFlare 保护。

不确定下一步该怎么做,CloudFlare 代理暂时没有问题。

【问题讨论】:

    标签: python cookies python-requests cloudflare


    【解决方案1】:

    您应该在“Cookie”键中使用字符串,而不是字典。它应该看起来像这样{"user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0", "Cookie": "cf_clearance=<some hash here>; cf_chl_2=<some hash here>; cf_chl_prog=x11; XSRF-TOKEN=<some hash here>; laravel_session=<some hash here>; __cf_bm=<some hash here>;"}

    【讨论】:

    • 抱歉,但我看不出这与我的一次尝试有什么不同。你是想说我放入标题对象的是字典,而不是字符串吗?这不是我发明的,我只是在寻找其他人如何做的例子并将其放入我的代码中。如果您可以扩大上下文,或者只是将实际编码部分的行放入我应该如何将标头放入请求中。
    【解决方案2】:

    完整的代码如下所示,但请记住,检查会持续 10-15 分钟,之后您将需要从浏览器获取新的 cookie。

    import requests
    h = {
    "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0", 
    "Cookie": "cf_clearance=<some hash here>; cf_chl_2=<some hash here>; cf_chl_prog=x11; XSRF-TOKEN=<some hash here>; laravel_session=<some hash here>; __cf_bm=<some hash here>;"
    }
    requests.get(url, headers=h)
    

    【讨论】:

    • 在对原始请求的响应中,站点返回新的 cookie 代码,我也可以将其用于下一个请求。我用 python 试了一下,还是不行。我在某处读到 CloudFlare 可以以某种方式识别 python 脚本,并且检测与 SSL 指纹有关。由于我尝试的 100% 失败率,我暂时把它放在一边,然后回到其他东西上。
    【解决方案3】:

    您知道如何获取诸如“cf_clearance”之类的标记以添加到标题 cookie 中吗?我做了几次尝试,但找不到如何取回它们?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2020-02-11
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 2018-01-01
    相关资源
    最近更新 更多