要用 Python 求解 reCAPTCHA v3 或 reCAPTCHA Enterprise:从 render= 参数中读取 site key,从 grecaptcha.execute 调用中读取 action,把它们连同页面 URL 和代理一起发送给求解 API,然后在两分钟内把返回的 token 作为 g-recaptcha-response(或网站自定义的字段)提交。Enterprise 的请求几乎完全相同,只多一个标记,因为两者的区别只在脚本和服务端校验方式。
本文会说明 token 实际证明了什么、v3 与 Enterprise 有何不同、如何在真实页面上找到这些参数,并给出完整的 Python 与 Node.js 代码。最后列出有效 token 仍被拒绝的原因,这正是大多数 reCAPTCHA v3 自动化失败的地方。
如果你想直接查看价格、成功率和请求调试台,而不是教程,请访问 reCAPTCHA v3 求解 API 页面。
简而言之: 向 https://api.nslsolver.com/solve 发送 type 为 "recaptchav3" 的 POST 请求,携带 site_key、url、action 与 proxy;页面加载 enterprise.js 时再加上 "enterprise": true,并在两分钟内使用 data["token"]。更喜欢用客户端库?同样的调用在 官方 Python 与 Node.js SDK中只需一个方法。
reCAPTCHA v3 是如何工作的?
reCAPTCHA v3 从不显示复选框或图片九宫格。页面加载 recaptcha/api.js?render=SITE_KEY,当用户执行值得保护的操作(登录、注册、结账)时,网站调用 grecaptcha.execute(SITE_KEY, {action: 'login'})。该调用返回一个 token,页面会把它和表单一起发送到自己的后端。
后端把 token 和密钥(secret)发送到 Google 的 siteverify 接口,得到 success、分数、action 名称、hostname 与时间戳。Google 对分数的描述是:1.0 表示“很可能是正常交互”,0.0 表示“很可能是机器人”。
Google 本身不会拦截任何人,如何处理分数由网站决定:Google 建议先以 0.5 作为阈值,再根据真实流量调整。因此有的网站接受 0.3,有的要求 0.7,或者把低分请求转入二次验证。
对自动化来说有两条关键规则。根据 Google 文档,token 有效期为两分钟,且只能校验一次。另外,action 只能包含字母数字、斜杠和下划线,且不能因用户而异,所以它们是可以直接从页面中读到的固定字符串。
reCAPTCHA Enterprise 与 v3 有什么区别?
reCAPTCHA Enterprise 是 Google Cloud 版本。在页面上它看起来几乎一样:site key 通常同样以 6L 开头,token 同样在浏览器中生成。区别在于脚本、JavaScript 命名空间,以及最重要的——后端如何校验 token。
| reCAPTCHA v3 | reCAPTCHA Enterprise | |
|---|---|---|
| 脚本 | recaptcha/api.js?render=KEY | recaptcha/enterprise.js?render=KEY |
| 前端调用 | grecaptcha.execute(key, {action}) | grecaptcha.enterprise.execute(key, {action}) |
| 后端校验 | siteverify(secret + response) | projects.assessments.create(Google Cloud API) |
| 网站得到的信息 | success、score、action、hostname、challenge_ts | tokenProperties(valid、invalidReason、action、hostname)+ riskAnalysis(score、reasons) |
| 分数 | 0.0 到 1.0 | 0.0 到 1.0(11 档;未绑定结算账号时为 4 档) |
| 原因代码 | 无 | AUTOMATION、UNEXPECTED_ENVIRONMENT、TOO_MUCH_TRAFFIC 等 |
| token 有效期 | 2 分钟,只能校验一次 | 2 分钟,只能使用一次 |
| NSLSolver 请求 | type: "recaptchav3" | type: "recaptchav3" + enterprise: true |
来源:Google reCAPTCHA v3 与 verify 文档、Google Cloud reCAPTCHA 评估文档及 REST 参考。
如何在页面上找到 site key 与 action?
你需要从目标页面获取三个值:site key、action 以及版本(v3 或 Enterprise)。在页面上打开开发者工具,按以下顺序查找:
- site key:reCAPTCHA 脚本(www.google.com/recaptcha/... 或 www.recaptcha.net/recaptcha/...)的 render= 参数。如果写的是 render=explicit,则 key 是 grecaptcha.execute 的第一个参数。
- 版本:出现 enterprise.js 和 grecaptcha.enterprise.execute 即为 Enterprise;api.js 和 grecaptcha.execute 则为标准 v3。
- action:execute 调用中的 action 值,例如 login、submit 或 checkout。它通常位于 JavaScript 打包文件中而非 HTML,可在 Sources 面板中搜索 "execute(" 或 "action:"。
- token 提交位置:在真实浏览器中提交一次表单并查看 Network 面板。请求会显示确切的字段名(g-recaptcha-response、recaptchaToken、captcha 等)和接口地址。
import re
import requests
html = requests.get("https://example.com/login", timeout=30).text
# Site key + edition: the render= parameter of api.js (v3) or enterprise.js
m = re.search(r"recaptcha/(api|enterprise)\.js\?render=([\w-]+)", html)
if m:
enterprise = m.group(1) == "enterprise"
site_key = m.group(2)
print("site_key:", site_key, "| enterprise:", enterprise)
# Action: the value passed to grecaptcha.execute / grecaptcha.enterprise.execute
actions = re.findall(r"execute\([^)]*?action\s*:\s*['\"]([\w/]+)['\"]", html)
print("actions:", actions)用 Python 求解 reCAPTCHA v3
拿到这三个值后,向 NSLSolver 的 /solve 接口发送一次 POST 即可获得 token。代理为必填项:reCAPTCHA 的评分部分依赖出口 IP,token 会通过你传入的代理生成,因此提交表单时也应使用同一个代理。平均求解耗时约 13 秒。
下面的示例会完成求解、检查错误响应({"success": false, "error": "..."}),并立即提交 token,以确保在两分钟有效期内使用。
import os
import requests
API_KEY = os.environ["NSLSOLVER_API_KEY"]
PROXY = "http://user:pass@host:port" # same exit IP for solving and submitting
PAGE_URL = "https://example.com/login" # page where reCAPTCHA runs
SITE_KEY = "6Lc..." # the render= value
ACTION = "login" # the action passed to grecaptcha.execute
def solve_recaptcha_v3(enterprise: bool = False) -> str:
payload = {
"type": "recaptchav3",
"site_key": SITE_KEY,
"url": PAGE_URL,
"action": ACTION,
"proxy": PROXY,
}
if enterprise:
payload["enterprise"] = True # the page loads recaptcha/enterprise.js
resp = requests.post(
"https://api.nslsolver.com/solve",
headers={"X-API-Key": API_KEY},
json=payload,
timeout=120,
)
data = resp.json()
if resp.status_code != 200 or data.get("success") is False:
raise RuntimeError(f"Solve failed ({resp.status_code}): {data.get('error')}")
return data["token"]
token = solve_recaptcha_v3(enterprise=False)
# Submit right away: the token expires after two minutes and works only once.
session = requests.Session()
session.proxies = {"http": PROXY, "https": PROXY}
r = session.post(
"https://example.com/login",
data={
"email": "me@example.com",
"password": "...",
"g-recaptcha-response": token, # or the field name the site really uses
},
timeout=30,
)
print(r.status_code)用 Python 求解 reCAPTCHA Enterprise
对于 Enterprise 网站,其他一切保持不变,只需开启 enterprise 标记:在上面的脚本中调用 solve_recaptcha_v3(enterprise=True)。对 Enterprise 的 key 发送标准 v3 请求(或反过来),是 token 被网站拒绝的常见原因。
使用官方 Python SDK 时,同样的请求只需一次方法调用:
# pip install nslsolver
from nslsolver import NSLSolver
client = NSLSolver("nsl_...")
token = client.recaptcha_v3(
site_key="6Lc...",
url="https://example.com/login",
action="login",
enterprise=True, # reCAPTCHA Enterprise (enterprise.js)
proxy="http://user:pass@host:port",
)
print(token)用 Node.js 求解 reCAPTCHA v3
Node.js 版本使用内置 fetch 发起求解请求,并用 undici 的 ProxyAgent 通过同一代理提交表单。可选的 user_agent 字段可让求解浏览器与你自己客户端发送的 User-Agent 保持一致。
// solve.mjs (Node.js 18+, ES module)
// npm install undici -> only needed to send the form through the proxy
import { fetch as proxiedFetch, ProxyAgent } from "undici";
const API_KEY = process.env.NSLSOLVER_API_KEY;
const PROXY = "http://user:pass@host:port";
const PAGE_URL = "https://example.com/login";
async function solveRecaptchaV3({ siteKey, action, enterprise = false, userAgent }) {
const body = {
type: "recaptchav3",
site_key: siteKey,
url: PAGE_URL,
action,
proxy: PROXY,
};
if (enterprise) body.enterprise = true; // page loads recaptcha/enterprise.js
if (userAgent) body.user_agent = userAgent; // optional, match your own client
const resp = await fetch("https://api.nslsolver.com/solve", {
method: "POST",
headers: { "Content-Type": "application/json", "X-API-Key": API_KEY },
body: JSON.stringify(body),
signal: AbortSignal.timeout(120_000),
});
const data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error("Solve failed (" + resp.status + "): " + data.error);
}
return data.token;
}
const token = await solveRecaptchaV3({ siteKey: "6Lc...", action: "login", enterprise: true });
// Use it within two minutes, once, through the same proxy.
const res = await proxiedFetch("https://example.com/api/login", {
method: "POST",
dispatcher: new ProxyAgent(PROXY),
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email: "me@example.com", password: "...", recaptchaToken: token }),
});
console.log(res.status);token 应该放在哪里?
没有通用的字段名。传统表单集成会以 g-recaptcha-response 提交 token,这也是 Google 为 Enterprise 表单提交记录的 POST 参数。单页应用通常会以自定义名称放在 JSON 请求体或请求头中,所以花一分钟在 Network 面板里查看一次真实提交非常值得。
- HTML 表单:添加或填写名为 g-recaptcha-response 的字段,并附上表单通常发送的其他所有字段(CSRF token、隐藏输入框)。
- JSON API:把 token 放在前端使用的键中,例如 {"recaptchaToken": "..."},并保持相同的请求头与 Cookie。
- 浏览器自动化(Playwright、Selenium):用 token 调用网站自身的提交处理函数,或在提交表单前设置隐藏输入框。
- 每个请求一个 token。下一次登录或提交表单时需重新求解,因为已使用的 token 会被拒绝。
为什么有效的 reCAPTCHA token 仍会被拒绝?
token 可能是真实有效的,却仍然通不过网站的检查。后端看到的不只是“有效或无效”,以下任何一个字段都可能让一个本来没问题的 token 失败:
- action 不对。siteverify 会返回 action,Google 也要求网站所有者核对它。在 Enterprise 上,与 expectedAction 不一致可能产生 UNEXPECTED_ACTION 无效原因。请传入 execute 调用中完全相同的 action 字符串。
- 分数低于网站阈值。阈值由网站而非 Google 决定。任何求解器都无法保证具体分数;干净的代理与正确的 action 能带来最大的成功机会。
- 过期或重复使用。token 有效期两分钟且只能使用一次。v3 会返回 timeout-or-duplicate,Enterprise 会返回 EXPIRED 或 DUPE。
- 域名不同。响应中包含生成 token 的 hostname,对于该 key 不允许的域名,Enterprise 会返回 DOMAIN_MISMATCH。请始终传入真实的页面 URL,而不是其他子域名。
- key 或版本不对。为 Enterprise key 生成 v3 token(或为其他 key 生成的 token)都会失败,Enterprise 会返回 KEY_MISMATCH。
- IP 或 User-Agent 不一致。网站可能把访客 IP 传给 Google(siteverify 的 remoteip,Enterprise 的 userIpAddress 与 userAgent),或自行比对。稳妥起见,请通过求解时使用的同一代理提交。
reCAPTCHA v3 token 问题排查
根据现象定位原因。下表中的 API 错误来自 NSLSolver,其他错误来自目标网站,或由网站转达的 Google 校验结果。
| 现象 | 可能原因 | 解决方法 |
|---|---|---|
| timeout-or-duplicate / EXPIRED / DUPE | token 超过 2 分钟,或被使用了两次 | 提交前一刻再求解;每个请求使用一个 token |
| 网站提示“验证码失败”,但 API 已返回 token | action 不对,或分数低于网站阈值 | 复制完全一致的 action;使用干净的住宅代理 |
| KEY_MISMATCH / invalid-input-response | site key 错误,或混淆了 v3 与 Enterprise | 重新读取 render=;仅在 enterprise.js 时开启 enterprise |
| DOMAIN_MISMATCH / hostname 不符 | url 指向了其他域名或子域名 | 传入运行 reCAPTCHA 的页面的完整 URL |
| 浏览器中可用,脚本中失败 | IP 或 User-Agent 不同,缺少 Cookie 或字段 | 通过同一代理提交;回放所有表单字段 |
| API 400 | 字段缺失或无效(常见于代理格式) | 使用 protocol://user:pass@host:port 格式 |
| API 402 | 余额不足 | 充值;求解失败永不计费 |
| API 429 / 503 | 达到每分钟上限,或求解未完成 | 等待几秒后重试;两者均不计费 |
仅对成功的求解按每 1,000 次 $1.00 计费。校验错误、429 与 503 响应均不收费。
只在允许的场景中使用
reCAPTCHA 保护的是真实的账户与真实的交易。请只在以下场景使用自动求解:你自己的网站、针对你自己表单的 QA 与压力测试,以及条款允许自动化的网站。其他情况下,请先向网站所有者申请 API 访问权限。