当 Cloudflare 将访问者标记为可疑时,它会提供一个间隙质询页面。解决后,浏览器会收到一个 qa_session_cookie cookie,该 cookie 授予会话访问权限。本指南解释了挑战流程的运作方式以及 CaptchaAI 如何处理它。
Cloudflare 验证流程 页面如何运作
- 用户请求受 Cloudflare 保护的页面
- Cloudflare 评估风险信号(IP 信誉、标头、TLS 特征)
- 如果被标记,Cloudflare 将返回 403 或 503 以及 JavaScript 挑战页面
- 挑战页面运行浏览器检查并可能显示 Turnstile 小部件
- 成功后,Cloudflare 设置
qa_session_cookie并重定向到原始 URL - 带有 cookie 的后续请求将毫无疑问地通过
qa_session_cookie cookie
| 财产 | 细节 |
|---|---|
| 姓名 | qa_session_cookie |
| 领域 | 目标站点域(例如 .example.com) |
| 小路 | / |
| 寿命 | 通常为 30 分钟至 24 小时 |
| 仅HTTP | 是的 |
| 安全的 | 是(仅限 HTTPS) |
| 同一站点 | 没有任何 |
cookie 与几个因素有关:
- IP 地址 — 使用来自不同 IP 的 cookie 通常会失败
- 用户代理 — 必须与解决挑战期间使用的 UA 相匹配
- TLS 特征 — 一些配置绑定到 TLS ClientHello
挑战类型
Cloudflare 服务于不同的挑战级别:
| 挑战 | 响应码 | 用户交互 | 描述 |
|---|---|---|---|
| JS 挑战 | 503 | 没有任何 | 仅 JavaScript 执行 |
| 管理挑战 | 403 | 或许 | Cloudflare 决定 — 可能会显示 Turnstile 或默默通过 |
| 互动挑战 | 403 | 是的 | 始终显示Turnstile小部件 |
CaptchaAI 通过 Cloudflare 验证流程 求解器求解所有三种类型。
使用 CaptchaAI 解决
Python
import requests
import time
API_KEY = "YOUR_API_KEY"
TARGET_URL = "https://example.com/protected-page"
# Submit Cloudflare 验证流程 task
resp = requests.post("https://ocr.captchaai.com/in.php", data={
"key": API_KEY,
"method": "turnstile",
"sitekey": "0x0000000000000000000000", # may be generic for CF challenge
"pageurl": TARGET_URL,
"json": "1",
}).json()
if resp["status"] != 1:
raise Exception(f"Submit error: {resp['request']}")
task_id = resp["request"]
print(f"Task ID: {task_id}")
# Poll for result
for _ in range(30):
time.sleep(5)
result = requests.get("https://ocr.captchaai.com/res.php", params={
"key": API_KEY, "action": "get", "id": task_id, "json": "1"
}).json()
if result["status"] == 1:
token = result["request"]
print(f"Token received: {token[:50]}...")
break
if result["request"] != "CAPCHA_NOT_READY":
raise Exception(f"Error: {result['request']}")
JavaScript
const axios = require('axios');
const API_KEY = 'YOUR_API_KEY';
const TARGET_URL = 'https://example.com/protected-page';
const submit = await axios.post('https://ocr.captchaai.com/in.php', null, {
params: {
key: API_KEY,
method: 'turnstile',
sitekey: '0x0000000000000000000000',
pageurl: TARGET_URL,
json: 1,
}
});
const taskId = submit.data.request;
// Poll
let token = null;
for (let i = 0; i < 30; i++) {
await new Promise(r => setTimeout(r, 5000));
const poll = await axios.get('https://ocr.captchaai.com/res.php', {
params: { key: API_KEY, action: 'get', id: taskId, json: 1 }
});
if (poll.data.status === 1) {
token = poll.data.request;
break;
}
}
console.log(`Token: ${token.substring(0, 50)}...`);
使用已解决的令牌
解决后,将token注入挑战页面即可获得qa_session_cookie:
# With Selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(TARGET_URL) # Load the challenge page
# Inject token into Turnstile response field
driver.execute_script("""
const input = document.querySelector('input[name="cf-turnstile-response"]');
if (input) input.value = arguments[0];
// Trigger form submit or callback
const form = document.querySelector('form');
if (form) form.submit();
""", token)
# Wait for redirect and extract cookies
import time
time.sleep(3)
cookies = driver.get_cookies()
qa_session_cookie = next(
(c for c in cookies if c['name'] == 'qa_session_cookie'), None
)
if qa_session_cookie:
print(f"qa_session_cookie: {qa_session_cookie['value'][:30]}...")
重复使用 cookie
session = requests.Session()
session.cookies.set("qa_session_cookie", qa_session_cookie["value"], domain=".example.com")
session.headers.update({
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
})
# Now access protected pages
resp = session.get("https://example.com/api/data")
print(resp.status_code) # 200
Cookie 的生命周期和刷新
qa_session_cookie cookie 过期。解决计划:
import datetime
def is_cookie_valid(cookie):
if not cookie:
return False
expiry = cookie.get("expiry", 0)
return datetime.datetime.now().timestamp() < expiry - 60 # 60s buffer
def get_or_refresh_clearance(driver, target_url, solve_func):
cookies = driver.get_cookies()
cf = next((c for c in cookies if c["name"] == "qa_session_cookie"), None)
if is_cookie_valid(cf):
return cf["value"]
# Re-solve
token = solve_func(target_url)
# ... inject and extract new cookie
故障排除
| 问题 | 原因 | 处理方式 |
|---|---|---|
| 解决后cookie被拒绝 | IP 不匹配 | 使用相同的代理来解决和请求 |
| Cookie 很快就会过期 | 由站点设置的短 TTL | 到期前重新解决;检查 expiry 字段 |
| 403 尽管 cookie 有效 | 用户代理不匹配 | 在解决和请求会话之间匹配 UA |
| 挑战页面未加载 | JavaScript 已禁用 | 使用完整的浏览器(Selenium、Puppeteer) |
常问问题
我可以在不同机器之间共享 qa_session_cookie 吗?
仅当它们共享相同的 IP 并且您匹配用户代理字符串时。 Cloudflare 通常将 cookie 与原始 IP 关联起来。
qa_session_cookie 持续多久?
通常为 30 分钟到 24 小时,具体取决于站点所有者的 Cloudflare 配置。
使用 CaptchaAI 解决 Cloudflare 验证流程 页面
获取您的 API 密钥:验证码网站。
相关指南
- Cloudflare Turnstile 站点密钥提取
- Turnstile、hCaptcha 与 reCAPTCHA
- 用于更好解决率的 Cookie