使用返回的用户代理和客户端提示
在向目标网站提交求解结果的请求中,使用返回的浏览器身份信息。根据求解结果设置这两个请求头,并通过求解时成功的代理发送该请求。
本页内容
替换现有的浏览器请求头
| HTTP 请求头 | 已就绪的求解结果中的值 |
|---|---|
| User-Agent | solution.userAgent |
| sec-ch-ua | solution.secChUa |
不要保留抓取程序或现有 HTTP 会话中的随机用户代理。这样会用一个无关的浏览器身份替换求解结果返回的身份。只更新 sec-ch-ua 会导致两个请求头描述不同的浏览器。
替换旧值,而不是追加重复的请求头。检查会话默认设置或请求构建中间件,确认它们不会在请求发送前覆盖这些值。
使用成功的代理
从 ready 结果开始;pending 和 solving 不包含可用的求解结果。SDK 的 solve 方法会等待结果就绪,或抛出错误。
对于使用单个 proxy 提交的任务,在目标请求中复用该代理。如果提供了 proxyCandidates,请使用 Python 的 successful_proxy(original_task, ready_result) 或 JavaScript 的 successfulProxy(originalTask, readyResult) 选择成功的代理。不要直接使用第一个候选代理。
以下示例从 request 的 proxy 字段读取出站代理。对于使用候选代理的任务,请在发送请求前,将辅助函数选出的值放入该字段。具体设置请参阅代理候选项选择。
使用 Python 或 Node.js 发送目标请求
这些示例提交 reCAPTCHA v3 Enterprise 令牌。在 Python 中,solution 是已就绪结果中的求解结果字典。在 Node.js 中,result 是已就绪的任务。两个示例都显式设置返回的请求头,并通过所选代理发送请求。
# Continue after client.solve(). Install requests separately: pip install requests.
import requests
# This example endpoint accepts the standard reCAPTCHA form field.
# Use your application's real URL, form fields, CSRF token and session.
response = requests.post(
"https://accounts.example.com/login",
data={"g-recaptcha-response": solution["token"]},
headers={
"User-Agent": solution["userAgent"],
"sec-ch-ua": solution["secChUa"],
},
proxies={"http": request["proxy"], "https": request["proxy"]},
timeout=30,
allow_redirects=False,
)
# Inspect the application's response to determine whether it accepted the request.
print("Application HTTP status:", response.status_code)// Continue after client.solve(). Install undici separately: npm install undici@7.
import { fetch, ProxyAgent } from 'undici';
const dispatcher = new ProxyAgent(request.proxy);
try {
// Replace the URL and form fields with your application's contract.
const response = await fetch('https://accounts.example.com/login', {
method: 'POST',
dispatcher,
redirect: 'manual',
headers: {
'user-agent': result.solution.userAgent,
'sec-ch-ua': result.solution.secChUa,
},
body: new URLSearchParams({
'g-recaptcha-response': result.solution.token,
}),
signal: AbortSignal.timeout(30_000),
});
console.log('Application HTTP status:', response.status);
await response.arrayBuffer();
} finally {
await dispatcher.close();
}将示例登录 URL 替换为预期目标 URL,并使用该应用的实际表单字段、CSRF 令牌和会话。示例假设应用通过 g-recaptcha-response 接收令牌;返回的浏览器请求头应放在 HTTP 请求头中,而不是表单正文中。
如果目标拒绝请求,请检查 HTTP 客户端实际发出的内容:令牌值、两个浏览器请求头、代理和目标 URL。任务处于 ready 状态表示已生成求解结果,而不是目标已接受提交。打印出的应用 HTTP 状态只是排查起点;请检查应用响应,确认实际成功或失败的结果。
运行您的第一个任务。
使用 $5 工作区额度开始体验。