Python
Python 验证码识别
Python 里有两条路:用官方 SDK 传文件路径,或自己发 HTTP 请求提交图片字节、Base64。两条路的结果字段一致,都要先看 accepted。
使用 Python SDK
SDK 目前从项目仓库安装:pip install -e .。只调用远程接口时不必安装本地推理引擎。异步程序使用 AsyncCaptchaOcrClient。
from captcha_ocr.sdk import CaptchaOcrClient
with CaptchaOcrClient("https://captchaocr.cn", api_key="ck_live_…") as client:
result = client.recognize(
"captcha.png",
charset="digits",
expected_length=4,
)
if result.accepted:
print(result.text, result.confidence)
else:
print(result.rejection_reason)用 requests 提交 Base64
import base64
import requests
image = base64.b64encode(open("captcha.png", "rb").read()).decode()
response = requests.post(
"https://captchaocr.cn/api/v1/recognize",
params={"charset": "digits", "expected_length": 4},
headers={"X-API-Key": "ck_live_…"},
json={"image": image},
timeout=30,
)
response.raise_for_status()
result = response.json()
if result["accepted"]:
print(result["text"])rejection_reason 可能是 empty、low_confidence 或 length_mismatch。余额不足时 HTTP 状态为 402,不要当成临时故障无限重试。
安装方式和异常类型见 Python SDK 文档。Java、Go、Node.js 和 Rust 示例见语言接入示例。识别对象的说明见验证码 OCR。