CloudflareBypassForScraping 失效,通常只有三类原因:你跑的是旧版本(项目自 2025 年以来换了三次引擎,去年的教程和报错已经对不上了);浏览器池在你的 IP 或代理下过不了挑战;或者它返回的 cf_clearance 被换了 User-Agent、IP 或 TLS 指纹去复用。解决思路是:拉取当前的 v3 Docker 镜像,求解和后续请求都走同一个粘性代理,带上返回的 user_agent,再用 curl_cffi 或内置的镜像模式发请求。
这个项目(sarperavci/CloudflareBypassForScraping,约 2,700 星,MIT 许可证,最近一次提交在 2026-11-28)是一个自托管服务,可以返回 Cloudflare clearance cookie、渲染后的 HTML 或 Turnstile token。下文依次介绍 3.0 版的工作方式、如何仅凭报错判断自己用的是哪个版本、带代码的具体修复方法,以及什么时候改用托管 API 更划算。
快速答案:按报错找修复方法
CloudflareBypassForScraping 的大多数失败都对应下面某一条报错或现象。先找到你遇到的那条,再看下文对应章节里的代码。
- Attempt 5: Verification page detected. Trying to bypass...(无限重复)
- 你在用旧版 CloudflareBypasser 类,它默认不限重试次数。 传入 max_retries,改用有头模式并换更干净的 IP;或者迁移到 v3 服务端。
- {"detail":"Failed to bypass Cloudflare protection"}
- /cookies 或 /html 返回 HTTP 500:浏览器池没能在时限内通过挑战。 改用固定 IP(非轮换)的住宅代理,把 CF_LOCALE 设成代理所在地区,带 bypassCookieCache=true 重试,并通过 /browsers 检查浏览器池是否卡死。
- /cookies 拿到了 cf_clearance,但 requests 仍然 403 或停在 "Just a moment..."
- 复用 cookie 时 User-Agent、IP 或 TLS 指纹和求解时不一致。 原样发送返回的 user_agent,/cookies 和后续请求走同一个代理,并用 curl_cffi 的 impersonate="chrome" 发请求;或者直接用镜像模式。
- {"detail":"x-hostname header is required for request mirroring"}
- 凡是不属于内置接口的路径,都会被当成镜像请求处理。 x-hostname 只写主机名(example.com,不要写 https://example.com);或者用 ?url= 调用 /cookies。
- {"detail":"Invalid or unsafe URL - localhost and private IPs are not allowed"}
- SSRF 防护拦截了 localhost 和内网地址。 只能请求公网域名:localhost 和内网 IP 段会被有意拒绝。
- {"detail":"Missing or invalid x-auth-token header"}(401)
- 服务端启动时设置了 --auth-token 或 CF_AUTH_TOKEN。 每个请求都要带 x-auth-token,包括 /browsers 和镜像请求。
- Page.goto: Timeout 10000ms exceeded / Failed to get Cloudflare clearance cookies
- 多半是还在用 Camoufox 时期的旧镜像,或者代理每次请求都换 IP(见 issue #115)。 拉取当前的 latest 镜像,并改用固定 IP 或粘性会话的代理。
- geoip2 is required for geoip=True / Looks like you launched a headed browser without having a XServer running.
- 这是 2026 年年中 CloakBrowser 和 Playwright 版本的报错(issue #125 和 #123)。 换成 v3 镜像;手动安装的话,加 --headless 或用 xvfb-run 启动。
报错原文摘自项目源码(cf_bypasser/server/routes.py、app.py、core/mirror.py)、旧版 CloudflareBypasser.py 以及文中引用的 GitHub issue,均于 2026-12-01 核实。
CloudflareBypassForScraping 在 2026 年怎么工作(3.0 版)
3.0 版于 2026-10-25 合并,在 8000 端口运行一个 FastAPI 服务,背后是一个可复用的无头 Brave 浏览器池,由 DrissionPage 驱动(依赖锁定为 DrissionPage >=4.1.1.4,<4.2)。每个请求借用一个浏览器,并在独立的一次性浏览器上下文中运行,所以每个请求都能用不同的代理,无需重启。非交互式挑战会自动通过;交互式 Turnstile 复选框会在 shadow DOM 里被定位并点击。
每个接口各管一件事。仓库没有发布 GitHub Releases;Docker 镜像 ghcr.io/sarperavci/cloudflarebypassforscraping:latest 同时提供 linux/amd64 和 linux/arm64 版本。
- /cookies?url=...&proxy=... 返回 cf_clearance 以及拿到它时所用的 user_agent。README 明确写着:两者必须一起发送,否则 Cloudflare 会拒绝这个 cookie。
- /html 返回渲染后的页面;/fetch 在已通过挑战的页面内部请求资源,原样返回字节(PDF、图片、JSON)。
- 镜像模式:请求任意其他路径并带上 x-hostname 请求头。服务端先拿 cookie,再用模拟 Chrome TLS 指纹的 curl_cffi 原样重放你的请求(方法、路径、查询参数、请求头、请求体)。
- /turnstile?url=...&sitekey=... 返回 Turnstile token(2026-11-02 新增),做法是注入一个只含组件的页面,而不是加载整个站点。
- /browsers 显示浏览器池状态(总数、空闲、忙碌、重启中)。
你用的是哪个版本?看报错就知道
大多数“用不了”的反馈,其实是不同时期的东西混在了一起:2024 年的教程、2025 年的 Docker 镜像、2026 年的 README。从提交历史看,项目先后用过四种引擎,各自的报错措辞都不一样。动手改之前,先确认你在哪一代。
- 2023 年至 2026 年年中,旧版类:CloudflareBypasser.py 加 test.py,用 DrissionPage 控制 Chrome。日志形如 "Attempt 1: Verification page detected. Trying to bypass..." 和 "Error clicking verification button"(issue #73、#101)。
- 2025 年底至 2026 年 6 月:基于 Camoufox 和 playwright-captcha 的服务端。日志里会出现 Camoufox、LeakWarning、Page.goto 超时和 NS_ERROR_NET_INTERRUPT(issue #104、#107、#115)。
- 2026 年 6 月至 10 月:CloakBrowser(2026-06-18 迁移,旧的 DrissionPage 代码在 2026-06-20 删除)。issue #125 里的 geoip2 报错就属于这一代。
- 2026-10-25 起,v3.0:重新回到 DrissionPage,但改成无头 Brave 浏览器池。报错是简短的 JSON detail,例如 "Failed to bypass Cloudflare protection"。
为什么会失效
- IP 信誉:即使浏览器很干净,用机房 IP 也可能失败。在 issue #115 里,维护者把反复超时归因于每次请求都换 IP 的轮换住宅代理,Cloudflare 始终无法建立信任,建议改用固定 IP。
- 无头检测:Docker 镜像默认以无头模式运行 Brave(CF_HEADLESS=true),而 server.py 自己对 --headless 参数的说明就写着 "Cloudflare may detect it"。
- cookie 绑定:cf_clearance 绑定了求解时的 User-Agent 和 IP。配置文档也提到,代理出口 IP 一变,cookie 就失效。
- 语言与地区不一致:浏览器语言和代理所在国家对不上,本身就是一个信号。v3 为此新增了 CF_LOCALE。
- 资源不足:按 docker-compose.yml 的说明,每个无头 Brave 约占 200 到 400 MB;内存或共享内存不够,浏览器就会崩溃、任务超时。
修复 1:正确运行 v3 服务端(Docker 与手动安装)
从当前镜像开始,并按 compose 文件的要求分配内存(上限 4 GB,shm_size 2g)。缓存默认关闭:不设 CF_CACHE_ENABLED=true 或 --enable-cache,每个请求都会重新求解一次挑战,既慢又消耗 IP 信誉。另外请设置 CF_AUTH_TOKEN 并只绑定 127.0.0.1,因为服务端会去抓取它收到的任何 URL。
手动安装时必须先装好 Brave(或用 CF_BROWSER_PATH 指定路径)。在 Docker 之外浏览器默认是有头模式,所以没有显示器的 Linux 服务器需要加 --headless 或用 xvfb-run 启动;issue #123 的提问者就是改用 Docker 镜像,才解决了开机自启服务的问题。issue #114 里 Podman 下的 "exec format error" 发生在 v3 之前,v3 的镜像同时发布了 amd64 和 arm64 版本。
# Current image (v3.0: a pool of headless Brave browsers driven by DrissionPage).
# Bind to localhost and set a token: the server fetches any URL it is sent.
docker pull ghcr.io/sarperavci/cloudflarebypassforscraping:latest
docker run -d --name cf-bypass \
-p 127.0.0.1:8000:8000 \
--shm-size=2g --memory=4g \
-e CF_AUTH_TOKEN=change-me \
-e CF_CACHE_ENABLED=true \
-e CF_LOCALE=en-US \
ghcr.io/sarperavci/cloudflarebypassforscraping:latest --browsers 3
# Is the browser pool up?
curl -H "x-auth-token: change-me" http://localhost:8000/browsers
# No semver tags were on the registry on 2026-12-01: record the digest that works
docker inspect --format '{{index .RepoDigests 0}}' \
ghcr.io/sarperavci/cloudflarebypassforscraping:latest
# Manual install instead (needs Brave; set CF_BROWSER_PATH if not auto-detected)
pip install -r server_requirements.txt
python server.py --browsers 3 --enable-cache # headed: needs a display
xvfb-run -a python server.py --browsers 3 --enable-cache # Linux server without a display修复 2:"Failed to bypass Cloudflare protection" 与超时
这个 500 表示任务结束前浏览器没有拿到 clearance。按下面的顺序逐项排查:
- 代理:使用固定 IP 或粘性会话代理(/cookies 用 proxy= 参数,镜像模式用 x-proxy 请求头)。代理地址必须以 http://、https://、socks4:// 或 socks5:// 开头;代理连不上时请求会直接失败,不会退回直连。
- 轮换代理池:如果必须轮换,设置 CF_IP_CHECK_ENABLED=true,服务端发现出口 IP 变化时会重新生成 cookie,而不是继续返回失效的旧 cookie。
- 语言地区:把 CF_LOCALE 设成代理所在地区(默认 en-US)。
- 超时:CF_CHALLENGE_TIMEOUT(30 秒)控制轮询点击复选框的时长,CF_JOB_TIMEOUT(120 秒)控制单个请求可以占用浏览器多久;超时后日志会提示该浏览器任务 "exceeded 120s; restarting that browser"。
- 旧 cookie:给 /cookies 加 bypassCookieCache=true,或给镜像请求加 x-bypass-cache: true,强制重新求解。
- 用干净的住宅 IP 仍然只在某一个站点失败:说明这个站点在给无头浏览器本身打分,再多重试也没用。
修复 3:cookie 在服务端有效,放进自己的代码就失效
如果 /cookies 返回了 cf_clearance,而你自己的客户端仍然 403 或停在 "Just a moment...",问题出在复用方式。原样发送返回的 user_agent,后续请求走你传给 /cookies 的同一个代理,并使用带 Chrome TLS 指纹的客户端。项目自带的示例用的就是 curl_cffi,而镜像模式正是在服务端替你做这件事。cookie 到底绑定了什么,见我们的 cf_clearance cookie 指南;指纹不一致的问题见 curl_cffi 遇到 Cloudflare 403。
issue #119 提到一个例外:有些站点在通过挑战后还会继续用 JavaScript 检查,即使 cookie 正确,纯 HTTP 重放也会失败。这类站点请改用 /html 或 /fetch,它们在浏览器内部执行。
# pip install curl_cffi
from curl_cffi import requests
SERVER = "http://localhost:8000"
AUTH = {"x-auth-token": "change-me"} # only if the server runs with CF_AUTH_TOKEN
TARGET = "https://protected-site.example/"
PROXY = "http://user:pass@sticky-proxy.example.com:8080" # sticky exit IP, no per-request rotation
# 1) Clearance cookies earned through the proxy you will scrape with
r = requests.get(
f"{SERVER}/cookies",
params={"url": TARGET, "proxy": PROXY},
headers=AUTH,
timeout=180,
)
r.raise_for_status()
data = r.json() # {"cookies": {"cf_clearance": "..."}, "user_agent": "..."}
# 2) Replay: same cookies, same User-Agent, same proxy, Chrome TLS fingerprint
s = requests.Session(impersonate="chrome", proxies={"http": PROXY, "https": PROXY})
s.headers["User-Agent"] = data["user_agent"]
s.cookies.update(data["cookies"])
page = s.get(TARGET, timeout=30)
print(page.status_code, page.headers.get("cf-mitigated"))
# Or let the server replay it (mirror mode): the path goes to the x-hostname host
m = requests.get(
f"{SERVER}/products?page=2",
headers={**AUTH, "x-hostname": "protected-site.example", "x-proxy": PROXY},
timeout=180,
)
print(m.status_code, len(m.content))修复 4:旧版 CloudflareBypasser 类无限循环
最初的这个类只检查页面标题里是否还有 "just a moment",然后点击复选框、休眠两秒。它默认 max_retries=-1,也就是永远不停,所以日志里会刷满 "Attempt N: Verification page detected"。请限制重试次数、改用有头模式,并把持续循环看作 IP 或指纹问题,而不是代码 bug。DrissionPage 本身的问题见 DrissionPage 过不了 Cloudflare。
# Legacy DrissionPage class (2023 to mid-2026). It was removed from the repo
# in June 2026; this only applies if you vendored CloudflareBypasser.py.
from DrissionPage import ChromiumOptions, ChromiumPage
from CloudflareBypasser import CloudflareBypasser
options = ChromiumOptions() # headed by default; on a server run under xvfb-run
driver = ChromiumPage(options)
driver.get("https://protected-site.example/")
# The default max_retries=-1 never gives up: cap it so a blocked IP fails fast
cf = CloudflareBypasser(driver, max_retries=5, log=True)
cf.bypass()
if "just a moment" in driver.title.lower():
raise RuntimeError("still on the challenge page: change IP or move to the v3 server")
print({c["name"]: c["value"] for c in driver.cookies()}.get("cf_clearance"), driver.user_agent)
driver.quit()什么时候该换成 API
如果服务器在你手里、请求量适中、也愿意维护浏览器池的健康,CloudflareBypassForScraping 很合适。但如果做完上面的修复后,同一个站点用干净代理仍然失败;为了每个站点拿一个 cookie,你在按每个浏览器 200 到 400 MB 规划服务器;或者每次引擎更换都要重新测试整条流水线,那就说明你在错误的层面上较劲。如果还在比较其他自托管工具,可以看我们的开源 Turnstile 与 Cloudflare 求解器对比,以及 cf-clearance-scraper 失效排查。
托管求解服务只替换挑战这一步:它通过 HTTP 返回 cf_clearance 和匹配的 User-Agent,爬虫的其余部分保持不变。请只对你拥有或已获明确授权访问的网站做自动化,并遵守对方的条款和频率限制。
NSLSolver 示例:不用浏览器池也能拿到 cf_clearance
NSLSolver 的 Cloudflare Challenge 求解 API 接收页面 url、挑战的 site_key 和你的代理,返回 cookies(包含 cf_clearance)以及需要通过同一代理复用的 user_agent。求解在服务端完成,完全基于请求,你这边和我们这边都不用浏览器。请求里不要传 user_agent:clearance 是为求解端的 iPhone Safari 浏览器签发的。Challenge 平均约 3.3 秒,每 1,000 次 1.00 美元(价格);失败的求解(503)不计费,使用 Google、Gmail 或 Proton 注册即送免费试用额度,无需信用卡。
如果是表单上的 Turnstile 组件,就用 type turnstile,传 site_key 和 url(约 250 毫秒,每 1,000 次 0.80 美元),把 token 作为 cf-turnstile-response 提交。NSLSolver 不支持 reCAPTCHA v2、hCaptcha、图片验证码和 DataDome。
import requests
API_KEY = "nsl_YOUR_API_KEY"
TARGET = "https://protected-site.example/"
PROXY = "http://user:pass@sticky-proxy.example.com:8080" # same exit IP for solve AND replay
SITE_KEY = "0x4AAAAAAA..." # the challenge widget id (0x...) from the challenge page
# 1) One HTTP call replaces the browser pool: no Brave, no Docker, no Xvfb
resp = requests.post(
"https://api.nslsolver.com/solve",
headers={"X-API-Key": API_KEY},
json={
"type": "challenge",
"url": TARGET,
"site_key": SITE_KEY,
"proxy": PROXY,
# user_agent omitted: the clearance is issued for the solver's iPhone
# Safari browser, and the matching UA comes back in the response
},
timeout=120,
)
resp.raise_for_status()
data = resp.json()
# 2) Replay cf_clearance with the returned user_agent through the SAME proxy
session = requests.Session()
session.proxies = {"http": PROXY, "https": PROXY}
session.cookies.update(data["cookies"]) # includes cf_clearance
session.headers["User-Agent"] = data["user_agent"]
page = session.get(TARGET, timeout=30)
print(page.status_code, page.headers.get("cf-mitigated"))相关指南
- Cloudflare Challenge 求解 API:一次调用拿到 cf_clearance 和 user_agent。
- DrissionPage 过不了 Cloudflare:v3.0 所基于的库本身的报错与修复。
- FlareSolverr 在 2026 年失效:另一个自托管 cookie 服务,以及它为什么超时。
- cf_clearance cookie 详解:它为什么绑定 IP 和 User-Agent,以及如何复用。
- NSLSolver 价格:按成功求解计费,使用 Google、Gmail 或 Proton 注册送免费试用额度。
参考来源(2026-12-01 核实)
- CloudflareBypassForScraping README(v3.0、各接口、镜像模式、Turnstile)
- docs/USAGE.md(安装、Docker、控制请求头、server.py 参数)
- docs/CONFIGURATION.md(CF_* 环境变量、代理、缓存)
- docker-compose.yml(内存上限、shm_size、每个浏览器的内存占用)
- cf_bypasser/server/routes.py(错误响应)
- 提交历史(v3.0 合并、迁移到 CloakBrowser、删除旧代码)
- 旧版 CloudflareBypasser.py(max_retries=-1 循环),提交 99070f3
- Issue #115:使用轮换代理时超时
- Issue #119:403、镜像路径与 cookie 复用
- Issue #125:CloakBrowser 时期镜像的 geoip2 报错