爬虫:绕过5秒盾Cloudflare和DDoS-GUARD

本文章仅供技术研究参考,勿做它用!

5秒盾的特点

html 复制代码
<title>Just a moment...</title>

返回的页面中不是目标数据,而是包含上面的代码:Just a moment...

或者第一次打开网页的时候:

这几个特征就是被Cloudflare保护的网站,熟称5秒盾。

免费版5秒盾的绕过方法

安装cloudscraper

bash 复制代码
pip3 install cloudscraper
python 复制代码
import cloudscraper
scraper = cloudscraper.create_scraper()
resp = scraper.get('目标网站').text

将代码中的requests 替换成scraper即可,其他和requests一样正常使用。

付费版5秒盾的绕过方法

如果网站部署的是付费版的5秒盾,用上述的cloudscraper就无法绕过,会返回下面的内容

Detected a Cloudflare version 2 (aptcha challengeeature is not available in the operce (free) version

付费版本绕过方法:docker 部署 flaresolverr 。

绕盾大致过程就是使用flaresolverr来代理我们的request请求,flaresolverr启动模拟浏览器绕盾成功之后我们保存好所需的cookies等参数,后续的requests就使用这些参数直接请求目标页面即可。如果请求的时候发现又被拒绝了,就重新启动绕盾步骤。

部署flaresolverr,本文章中的docker 和 flaresolverr 部署在爬虫程序同一台机器上。

linux 指令:

docker run -d --name=flaresolverr -p 8191:8191 -e LOG_LEVEL=info --restart unless-stopped ghcr.io/flaresolverr/flaresolverr:latest

这里的代理端口设置的是 8191

部署完毕后使用curl 指令进行测试,不是本机部署请更换localhost 为flaresolverr 机器IP

curl -L -X POST 'http://localhost:8191/v1' -H 'Content-Type: application/json' --data-raw '{"cmd": "request.get","url":"https://你的目标网站/","maxTimeout": 160000}'

这里有个注意点就是如果部署的机器性能比较差、目标网站性能差、或者网络通路不良等情况,maxTimeout 的值要设置 的足够大(ms),不然会失败,我在实测一个网站的时候最大一次绕盾过程用了两分钟,例子代码中设置了160秒超时。

附上绕盾用的python代码:

python 复制代码
    def 绕过5秒盾(self,xxxx):
        print("破盾中...")
        urlServer = "http://localhost:8191/v1"
        payload = json.dumps({
            "cmd": "request.get",
            "url":'https://www.目标网站.com',
            "maxTimeout": 160000
            })
        headers = {
            'Content-Type': 'application/json'
            }

        response = requests.post(urlServer, headers=headers, data=payload)
        # print(response.status_code)
        # print(response.json()['solution']['cookies'])
        if response.status_code==200:
            userAgent = response.json()['solution']['userAgent']
            for item in response.json()['solution']['cookies']:
                self.cookies[item["name"]] = item["value"]
            self.UA = userAgent
            print("破盾成功")
            return response
        print("绕过5秒盾错误!!!")

获取到绕盾成功的cookies ,之后的请求都使用requests 携带此cookie 直接请求即可,不需要每个请求都使用绕盾的方式。

因为flaresolverr启动绕盾浏览器会占用大量机器资源。

参考内容:

https://www.163.com/dy/article/HUM0G2U505561QYO.html

https://www.5axxw.com/wiki/content/df5u4r

相关推荐
清水白石00816 分钟前
突破并行瓶颈:Python 多进程开销全解析与 IPC 优化实战
开发语言·网络·python
Lupino2 小时前
IoT 平台可编程化:基于 Pydantic Monty 构建工业级智能自动化链路
python
清水白石0083 小时前
突破性能瓶颈:深度解析 Numba 如何让 Python 飙到 C 语言的速度
开发语言·python
yunhuibin4 小时前
AlexNet网络学习
人工智能·python·深度学习·神经网络
喵手5 小时前
Python爬虫实战:增量爬虫实战 - 利用 HTTP 缓存机制实现“极致减负”(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·增量爬虫·http缓存机制·极致减负
一个处女座的程序猿O(∩_∩)O5 小时前
Python异常处理完全指南:KeyError、TypeError、ValueError深度解析
开发语言·python
was1725 小时前
使用 Python 脚本一键上传图片到兰空图床并自动复制链接
python·api上传·自建图床·一键脚本
好学且牛逼的马5 小时前
从“Oak”到“虚拟线程”:JDK 1.0到25演进全记录与核心知识点详解a
java·开发语言·python
shangjian0076 小时前
Python基础-环境安装-Anaconda配置虚拟环境
开发语言·python
codeJinger6 小时前
【Python】函数
开发语言·python