当 Crawlee 在 Cloudflare 站点上输出“Request blocked - received 403 status code.”时,它的会话池其实在按设计工作:401、403 和 429 被视为拦截,当前会话被淘汰,请求换一个会话重试。拦截本身有三种原因:你的 HTTP 爬虫的指纹或 IP 看起来像自动化程序;Cloudflare 下发了 CheerioCrawler 和 got-scraping 无法执行的 JavaScript 挑战;或者防火墙规则(错误 1020)直接拒绝了你。
按这个顺序修复:先确认属于哪种情况;更换 HTTP 客户端和代理(got-scraping 已停止维护);把挑战页交给配合 Camoufox 与 handleCloudflareChallenge 的 PlaywrightCrawler;或者在 HTTP 爬虫里重放 cf_clearance cookie。Crawlee 的行为引自 3.x 版源码和文档(crawlee 3.18.2),核对日期 2026-12-01。
快速答案:你的 Crawlee 日志是什么意思
找到你日志中的那一行,再跳到对应步骤。
- Request blocked - received 403 status code.
- 会话池把 401、403、429 视为拦截(blockedStatusCodes 默认值)。 会话被淘汰、请求被重试。与其加大重试次数,不如先查清为什么被拦(第 1 步)。
- Detected a session error, rotating session...
- SessionError;它计入 maxSessionRotations(默认 10),而不是 maxRequestRetries(默认 3)。 如果每个会话都失败,多轮换也没用;问题在指纹、IP 或挑战。
- Request failed and reached maximum retries.
- 所有重试也都被拦了。 诊断方法相同:检查 403 响应中的 cf-mitigated 头。
- Cloudflare challenge failed, found selectors: ...
- retryOnBlocked 等待后仍发现 Cloudflare 挑战页面结构。 单靠浏览器爬虫没能通过。试试 Camoufox 加 handleCloudflareChallenge,或使用 cf_clearance cookie。
- Detected Cloudflare challenge at <url>, trying to solve it.
- handleCloudflareChallenge(Crawlee 3.13.0+)正在点击挑战。 如果随后抛出“Blocked by Cloudflare when processing <url>”,说明这是拦截页而不是挑战。
- 403“Sorry, you have been blocked”/ Error 1020
- 防火墙规则,没有可求解的东西。 更换 IP 或地区,或联系站点所有者。
- 跑一阵子后出现 429
- 触发限速。 降低 maxConcurrency,并通过 ProxyConfiguration 分散请求。
- require got-scraping 时报 Error [ERR_REQUIRE_ESM]
- got-scraping 4.x 只支持 ESM。 改用 import;更好的做法是迁移到 impit,因为 got-scraping 已停止维护。
日志原文引自 Crawlee 3.x 源码(basic-crawler、browser-crawler、playwright-utils、errors);默认值来自 SessionPool API 文档;got-scraping 状态来自其 README,核对日期 2026-12-01。
被 Cloudflare 拦截时 Crawlee 会做什么
每个 Crawlee 爬虫都运行在会话池之上。默认情况下(useSessionPool: true、persistCookiesPerSession: true),每个会话带有自己的 cookie,配置了 ProxyConfiguration 时还会绑定自己的代理。当响应状态码属于 blockedStatusCodes(默认 [401, 403, 429])时,BasicCrawler 会抛出“Request blocked - received 403 status code.”,淘汰该会话并用另一个会话重试请求。会话错误计入 maxSessionRotations(默认 10),与 maxRequestRetries(默认 3)分开计算。
这个循环能很好地处理偶尔出现的坏 IP。但它不会改变你的指纹,也无法让 HTTP 爬虫运行 JavaScript:Crawlee 文档写明,基于 HTTP 的爬虫通常无法执行客户端 JavaScript。如果每个会话都收到同样的 403,多轮换只会白白消耗代理。
第 1 步:在 Crawlee 淘汰会话之前看到 403
想快速诊断时,把 blockedStatusCodes 设为空,让 403 进入你的处理函数,并关闭重试。Cloudflare 会给挑战页加上 cf-mitigated: challenge 响应头和“Just a moment...”标题;拦截页(1020)和限速(429)长得不一样,修法也不同。诊断完记得恢复默认值。
import { CheerioCrawler } from 'crawlee';
const crawler = new CheerioCrawler({
// Let 401/403/429 reach the handler instead of
// "Request blocked - received 403 status code." (debugging only)
sessionPoolOptions: { blockedStatusCodes: [] },
maxRequestRetries: 0,
async requestHandler({ request, response, $, log }) {
const status = response.statusCode;
const mitigated = response.headers['cf-mitigated'];
const title = $('title').text();
log.info(request.url + ' status=' + status + ' cf-mitigated=' + mitigated + ' title=' + title);
if (mitigated === 'challenge' || title.includes('Just a moment')) {
log.warning('JS challenge: an HTTP crawler cannot pass it, see the browser or cf_clearance steps');
} else if ($('body').text().includes('1020')) {
log.warning('Firewall rule (1020): nothing to solve, change IP or ask the site owner');
} else if (status === 429) {
log.warning('Rate limited: lower maxConcurrency and spread over proxies');
}
},
});
await crawler.run(['https://target-site.com/']);第 2 步:替换 got-scraping 并修正代理
got-scraping 是 Crawlee 3.x 的默认 HTTP 客户端(GotScrapingHttpClient)。它的 README 开头如今写着“got-scraping is EOL”:不再提供更新和支持,Apify 建议新项目使用 impit。它目前仍能用(npm 4.2.2,2026 年 10 月),但不再变化的类浏览器请求头和 TLS 设置会一个月比一个月显得陈旧。got-scraping 的 issue #65“got-scraping inefficient against Cloudflare”在关闭前积累了 52 条评论。
Crawlee 提供了替代品 ImpitHttpClient(npm i @crawlee/impit-client),文档示例中带有 browser 选项。再配合 ProxyConfiguration 使用:Crawlee 的“Avoid getting blocked”指南指出,IP 地址和浏览器指纹是被拦截的两大主因,数据中心 IP 被挑战的概率远高于住宅 IP。对限速严格的网站,请降低 maxConcurrency。
// npm i @crawlee/impit-client
// got-scraping is end-of-life; impit is Apify's recommended replacement.
import { CheerioCrawler, ProxyConfiguration } from 'crawlee';
import { ImpitHttpClient } from '@crawlee/impit-client';
const crawler = new CheerioCrawler({
httpClient: new ImpitHttpClient({
browser: 'firefox', // browser profile impit imitates (example from the Crawlee docs)
}),
proxyConfiguration: new ProxyConfiguration({
proxyUrls: ['http://user:pass@proxy-1.example.com:8000', 'http://user:pass@proxy-2.example.com:8000'],
}),
maxConcurrency: 5,
async requestHandler({ request, $, log }) {
log.info(request.url + ' ' + $('title').text());
},
});
await crawler.run(['https://target-site.com/']);第 3 步:挑战页需要 PlaywrightCrawler
如果第 1 步显示 cf-mitigated: challenge,换什么 HTTP 客户端都没用,请把这些 URL 交给浏览器爬虫。Crawlee 在反拦截指南中针对 Cloudflare 给出的建议是:PlaywrightCrawler 配合 Camoufox(一个面向抓取的 Firefox 构建),并在导航后钩子中调用 handleCloudflareChallenge。同时关闭 Crawlee 自带的指纹注入(useFingerprints: false),以免与 Camoufox 的指纹冲突。
handleCloudflareChallenge 在 Crawlee 3.13.0(2025 年 3 月)加入,并在 3.18.0 和 3.18.1 中针对 Cloudflare 新的页面结构做了更新,所以如果你还在用较旧的 3.x,请先升级。源码注释说它“配合 camoufox 效果最好”。它会识别挑战、点击复选框,然后等待页面加载(sleepSecs,默认 10 秒)。选项允许你覆盖点击位置以及挑战页和拦截页的识别逻辑。
// npm i crawlee playwright camoufox-js (camoufox-js needs Node 22+)
// Verbatim pattern from Crawlee's "Avoid getting blocked" guide.
import { PlaywrightCrawler } from 'crawlee';
import { launchOptions } from 'camoufox-js';
import { firefox } from 'playwright';
const crawler = new PlaywrightCrawler({
postNavigationHooks: [
async ({ handleCloudflareChallenge }) => {
await handleCloudflareChallenge(); // Crawlee 3.13.0+
},
],
browserPoolOptions: {
// Disable the default fingerprint spoofing to avoid conflicts with Camoufox.
useFingerprints: false,
},
launchContext: {
launcher: firefox,
launchOptions: await launchOptions({ headless: true }),
},
async requestHandler({ page, log }) {
log.info(await page.title());
},
});
await crawler.run(['https://target-site.com/']);retryOnBlocked、指纹及其他开关
- retryOnBlocked: true(默认 false)会重试看起来像 Cloudflare 或谷歌限速页的页面。在浏览器爬虫中,遇到 Cloudflare 的 403 时它会等待约 5 秒,失败后报“Cloudflare challenge failed, found selectors: ...”。不要与自定义的 blockedStatusCodes 同时使用:Crawlee 会警告“the 'retryOnBlocked' feature might not work as expected”。
- 指纹:使用 Chromium 或原版 Firefox 时,保留 browserPoolOptions.useFingerprints(默认开启),并把 fingerprintOptions 限定为一种真实的浏览器和操作系统,而不是随机混搭。
- 已知问题:如果症状相符,值得看看 crawlee issue #3124、#3629(“handleCloudflareChallenge not working as intended”,2026 年 6 月关闭)以及 #2027(retryOnBlocked 通过后状态码仍是 403)。
- 版本:crawlee 3.18.2 是当前的 3.x 版本;v4 处于候选发布阶段,并调整了部分拦截逻辑,所以请固定版本。Crawlee for Python(1.11.5)有自己的 API,本文代码均为 JavaScript。
第 4 步:在 HTTP 爬虫中重放 cf_clearance
浏览器每页的成本都很高。常见的折中方案是:每个 IP 获取一次 cf_clearance,然后用 CheerioCrawler 抓取,同时发送这个 cookie、获取它时的完全相同的 User-Agent,并走同一个代理。这个 cookie 绑定了什么、能用多久,见 cf_clearance cookie 指南。
示例通过 NSLSolver 的 challenge 类型获取 cookie(求解在服务端完成,你的机器上不需要浏览器),并在导航前钩子中注入。需要坦白一点:clearance 是为 iPhone Safari 浏览器签发的,而 got-scraping 和 impit 呈现的是 Chrome 或 Firefox 风格的 TLS。如果某个网站会检查这种对应关系并再次挑战,请改用带 Safari iOS 指纹的客户端抓取这些页面(curl_cffi 的 safari_ios 目标,见 curl_cffi 遇 Cloudflare 403),或者把它们留给浏览器爬虫。
// Node 18+ (built-in fetch). Get cf_clearance once, replay it from CheerioCrawler.
import { CheerioCrawler, ProxyConfiguration } from 'crawlee';
const TARGET = 'https://target-site.com/';
const PROXY = 'http://user:pass@sticky-proxy.example.com:8080'; // same exit IP for solve AND replay
const res = await fetch('https://api.nslsolver.com/solve', {
method: 'POST',
headers: { 'X-API-Key': process.env.NSLSOLVER_API_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({
type: 'challenge',
url: TARGET,
site_key: '0x4AAAAAAA...', // the challenge widget id
proxy: PROXY, // required for challenge
// no user_agent: the clearance is issued for an iPhone Safari browser
}),
});
const data = await res.json();
if (!res.ok || !data.success) throw new Error(res.status + ' ' + JSON.stringify(data));
const cookieHeader = Object.entries(data.cookies).map(([k, v]) => k + '=' + v).join('; ');
const crawler = new CheerioCrawler({
proxyConfiguration: new ProxyConfiguration({ proxyUrls: [PROXY] }),
persistCookiesPerSession: false, // we send the clearance cookie ourselves
preNavigationHooks: [
async (_ctx, gotOptions) => {
gotOptions.headers = {
...gotOptions.headers,
'user-agent': data.user_agent, // the UA the clearance was issued for
cookie: cookieHeader, // includes cf_clearance
};
},
],
async requestHandler({ request, response, log }) {
log.info(request.url + ' ' + response.statusCode + ' ' + response.headers['cf-mitigated']);
},
});
await crawler.run([TARGET]);什么时候求解 API 比继续调参更划算
当挑战成为瓶颈时就该用 API:每个会话都收到 cf-mitigated: challenge,你要抓取很多 Cloudflare 站点,或者每个 worker 跑一个 Camoufox 的成本已经超过数据本身的价值。NSLSolver 的 Cloudflare Challenge 求解服务 平均约 3.3 秒返回 cf_clearance 和 user_agent,每 1,000 次 $1.00。对于有 Turnstile 组件保护的表单,Turnstile 求解服务 约 250 毫秒返回 token,每 1,000 次 $0.80。只对成功的求解计费,使用 Google、Gmail 或 Proton 注册可获免费试用额度。1020 防火墙拦截任何人都无法求解。请只抓取你拥有或已获授权访问的网站。
相关指南
- Cloudflare Challenge 求解 API:返回 cf_clearance 和需要重放的 user_agent。
- Scrapy 遇 Cloudflare 403:Python Scrapy 中的同类问题。
- Camoufox 失效:Crawlee 推荐的浏览器仍被挑战时怎么办。
- Playwright stealth 失效:原版 Playwright 为何会被检测。
- 2026 年开源验证码求解工具:开源工具的全景地图。
来源(核对于 2026-12-01)
- Crawlee 文档:Avoid getting blocked(Camoufox + handleCloudflareChallenge)
- Crawlee 文档:HTTP 客户端(GotScrapingHttpClient、ImpitHttpClient)
- Crawlee API:SessionPoolOptions(blockedStatusCodes)
- Crawlee 文档:代理管理
- Crawlee 更新日志(3.x 分支)
- GitHub:apify/got-scraping(EOL 声明)
- got-scraping issue #65:对 Cloudflare 效果不佳
- Crawlee issue #3629:handleCloudflareChallenge 未按预期工作
- Crawlee issue #2027:retryOnBlocked 后状态码仍为 403
- npm:crawlee