使用 Python 求解 reCAPTCHA Enterprise

作者:solvethree阅读需 1 分钟

使用官方 Python SDK 请求 reCAPTCHA v3 Enterprise 令牌,然后使用返回的浏览器身份和同一个代理提交 solution.token。你需要 Python 3.10 或更高版本、工作区 API 密钥,以及目标站点已启用的配置文件。

本页内容

安装 SDK 并配置站点

安装官方 Python SDK:

安装 Python SDK
python -m pip install https://solvethree.com/downloads/solvethree_sdk-0.1.0-py3-none-any.whl
  1. 1.

    创建站点配置文件

    在 solvethree 控制台中设置目标主机名,并确保其配置文件已启用且可供你的工作区使用。在求解请求中发送主机名不会创建配置文件。如果尚未配置,请参阅站点配置文件设置。

  2. 2.

    设置 SOLVETHREE_API_KEY

    在运行脚本的 shell 或部署环境中,将 SOLVETHREE_API_KEY 设置为你的工作区 API 密钥。将密钥保存在服务器端,不要放在浏览器代码或提交到版本库的源文件中。

  3. 3.

    设置 SOLVETHREE_PROXY

    将 SOLVETHREE_PROXY 设置为求解和后续应用请求所使用的代理 URL。此示例使用单个代理,因此不要再添加 proxyCandidates。

请求 Enterprise 令牌

将此示例保存为 solve.py。将 accounts.example.com 替换为你已配置的主机名(不含协议或路径),并将 login 替换为目标页面使用的操作。

Python · solve.py
import os
from uuid import uuid4
from solvethree import Solvethree, SolvethreeError, TaskFailedError

client = Solvethree(os.environ["SOLVETHREE_API_KEY"])
request = {
    "site": "accounts.example.com",  # Use your configured hostname.
    "action": "login",              # Use the page's actual action.
    "proxy": os.environ["SOLVETHREE_PROXY"],
    "requestId": str(uuid4()),
}
try:
    result = client.solve(request, timeout=180)
except TaskFailedError as error:
    raise SystemExit(f"Task failed: {error.error_code}") from None
except SolvethreeError as error:
    # Keep these IDs so an interrupted request can be recovered.
    raise SystemExit(f"Recover request {error.request_id}, task {error.task_id}") from None

solution = result["solution"]
token = solution["token"]
user_agent = solution["userAgent"]
client_hints = solution["secChUa"]
print("Ready task:", result["id"])  # Do not print the token or proxy password.

请求默认为 enterprise-v3,因此不需要 recaptchaType 字段。不要添加 type 或 url;它们不是 reCAPTCHA 请求字段。

Solvethree 是同步的。client.solve 只创建一次任务,然后轮询,直到返回就绪结果或抛出异常。这里的 timeout=180 将客户端等待时间限制为 180 秒。成功后,脚本会提取令牌、用户代理和客户端提示,而不会输出敏感值。

向应用提交令牌

求解完成后,在同一个脚本中继续执行。按照示例中的说明单独安装 requests。将登录 URL 和表单数据替换为应用的实际请求,包括所需的凭据、CSRF 令牌和会话 Cookie。

Python requests · 应用已就绪的解决结果
# 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)

此示例假设应用通过 g-recaptcha-response 接收令牌。请检查实际请求,不要直接假定字段名。如果表单依赖现有会话,请通过该会话提交,而不是发起一个不带 Cookie 的新请求。

保留返回的 userAgent 和 secChUa,并使用求解时提供的同一个代理。切换 IP 或替换为不相关的浏览器身份可能导致请求失败。

任务就绪表示已生成解决方案,并不表示应用已接受它。请检查应用的响应和产生的会话;仅凭 HTTP 状态无法确定登录成功。如果应用拒绝令牌,请先检查发出的请求,再付费进行另一次求解:请求中必须包含 solution.token,而不是任务 ID 或旧令牌。请参阅诊断令牌被拒绝的问题。

恢复中断的求解,无需创建新任务

此示例在每次脚本启动时都会生成新的 requestId。如果需要在进程重启后恢复,请在调用 solve 前保存该 ID 和原始请求。直接重新运行脚本会创建新的逻辑请求。

  • 如果异常包含 task_id,请使用 get_task 恢复对该任务的轮询。不要仅因连接超时就创建替代任务。
  • 如果创建过程被中断,而你只有 request_id,请使用同一个 requestId 重新发送完全相同的任务请求。这会返回现有任务;复用该 ID 时更改任务输入会产生冲突。
  • 对于 TaskFailedError,请停止轮询该任务。在重试前检查其 errorCode 和 error;再次求解无法修复未作更改的无效输入。求解失败会退款。

SDK 不会自动重试创建任务。对于临时轮询失败,它会在求解截止时间内遵循 Retry-After。如需更精细地控制等待和恢复,请参阅任务轮询与超时。

运行您的第一个任务。

使用 $5 工作区额度开始体验。

创建工作区
使用 Python 求解 reCAPTCHA Enterprise · solvethree