自动化机器人处理可重复的任务 - 表单提交、帐户创建、数据输入、监控。验证码会中断这些工作流程。CaptchaAI 以编程方式解决验证码,因此您的机器人无需人工干预即可运行。
常见的自动化场景
| 设想 | 典型验证码 | CaptchaAI方法 |
|---|---|---|
| 表格提交 | reCAPTCHA v2 | method=userrecaptcha |
| 账户注册 | reCAPTCHA v2/v3 | method=userrecaptcha |
| 数据输入门户 | 图片验证码 | method=base64 |
| 预订/reservation | Cloudflare Turnstile | method=turnstile |
| API网关接入 | Cloudflare 验证流程 | method=cloudflare_challenge |
通用机器人框架
构建可重用的验证码解决机器人框架:
import requests
import time
import logging
logger = logging.getLogger(__name__)
class CaptchaBot:
def __init__(self, api_key):
self.api_key = api_key
self.session = requests.Session()
self.session.headers.update({
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
})
def solve(self, method, **params):
"""Solve any CAPTCHA type."""
params["key"] = self.api_key
params["method"] = method
resp = requests.get("https://ocr.captchaai.com/in.php", params=params)
if not resp.text.startswith("OK|"):
raise Exception(f"Submit error: {resp.text}")
task_id = resp.text.split("|")[1]
logger.info(f"Task submitted: {task_id}")
for _ in range(60):
time.sleep(5)
result = requests.get("https://ocr.captchaai.com/res.php", params={
"key": self.api_key, "action": "get", "id": task_id
})
if result.text == "CAPCHA_NOT_READY": continue
if result.text.startswith("OK|"): return result.text.split("|")[1]
raise Exception(f"Error: {result.text}")
raise TimeoutError("CAPTCHA solve timed out")
def submit_form(self, url, form_data, captcha_field="g-recaptcha-response",
site_key=None, captcha_method="userrecaptcha"):
"""Submit a form with CAPTCHA solving."""
if site_key:
if captcha_method == "userrecaptcha":
token = self.solve(captcha_method, googlekey=site_key, pageurl=url)
elif captcha_method == "turnstile":
token = self.solve(captcha_method, sitekey=site_key, pageurl=url)
form_data[captcha_field] = token
return self.session.post(url, data=form_data)
示例:表单提交机器人
bot = CaptchaBot("YOUR_API_KEY")
# Submit a contact form protected by reCAPTCHA
result = bot.submit_form(
url="https://example.com/contact",
form_data={
"name": "John Doe",
"email": "john@example.com",
"message": "Inquiry about your service"
},
site_key="6Le-wvkS...",
captcha_method="userrecaptcha"
)
print(f"Form submitted: {result.status_code}")
示例:多步骤工作流程机器人
def appointment_booking_bot(date, time_slot, user_info):
bot = CaptchaBot("YOUR_API_KEY")
# Step 1: Load booking page
page = bot.session.get("https://example.com/book")
# Step 2: Select date and time
resp = bot.session.post("https://example.com/book/select", data={
"date": date,
"time": time_slot
})
# Step 3: Fill personal info with CAPTCHA
result = bot.submit_form(
url="https://example.com/book/confirm",
form_data={
"name": user_info["name"],
"email": user_info["email"],
"phone": user_info["phone"],
"date": date,
"time": time_slot
},
site_key="6Le-wvkS...",
captcha_method="userrecaptcha"
)
return result.status_code == 200
# Run
success = appointment_booking_bot(
date="2025-02-15",
time_slot="10:00",
user_info={"name": "John Doe", "email": "john@example.com", "phone": "555-0100"}
)
示例:带有图像验证码的数据输入机器人
import base64
def data_entry_bot(entries, captcha_image_url):
bot = CaptchaBot("YOUR_API_KEY")
for entry in entries:
# Load the form page
page = bot.session.get("https://portal.example.com/entry")
# Download and solve image CAPTCHA
img = bot.session.get(captcha_image_url)
img_b64 = base64.b64encode(img.content).decode()
captcha_text = bot.solve("base64", body=img_b64)
# Submit entry
resp = bot.session.post("https://portal.example.com/entry", data={
**entry,
"captcha": captcha_text
})
logger.info(f"Entry submitted: {resp.status_code}")
time.sleep(random.uniform(2, 5))
Node.js 机器人框架
const axios = require("axios");
class CaptchaBot {
constructor(apiKey) {
this.apiKey = apiKey;
}
async solve(method, params) {
params.key = this.apiKey;
params.method = method;
const submit = await axios.get("https://ocr.captchaai.com/in.php", {
params,
});
const taskId = submit.data.split("|")[1];
while (true) {
await new Promise((r) => setTimeout(r, 5000));
const result = await axios.get("https://ocr.captchaai.com/res.php", {
params: { key: this.apiKey, action: "get", id: taskId },
});
if (result.data === "CAPCHA_NOT_READY") continue;
if (result.data.startsWith("OK|")) return result.data.split("|")[1];
throw new Error(result.data);
}
}
async submitForm(url, formData, siteKey, method = "userrecaptcha") {
const token = await this.solve(method, {
googlekey: siteKey,
pageurl: url,
});
formData["g-recaptcha-response"] = token;
return axios.post(url, new URLSearchParams(formData));
}
}
// Usage
const bot = new CaptchaBot("YOUR_API_KEY");
const result = await bot.submitForm(
"https://example.com/submit",
{ name: "John", email: "john@example.com" },
"6Le-wvkS..."
);
故障排除
| 问题 | 处理方式 |
|---|---|
| CAPTCHA 令牌被拒绝 | 解决后 120 秒内使用令牌 |
| 尽管令牌有效,仍检测到机器人 | 添加隐形标头和请求延迟 |
| 表单需要额外字段 | 检查表单源中的隐藏字段(CSRF 令牌) |
| 重复提交的速率受到限制 | 添加延迟并QA 测试会话 |
常问问题
自动化机器人可以处理任何验证码类型吗?
对于 CaptchaAI,是的。该 API 支持 reCAPTCHA(所有版本)、Cloudflare Turnstile、GeeTest、hCaptcha、图像验证码等。您的机器人框架只需要检测类型并调用正确的方法。
如何运行机器人 24/7?
使用调度工具(cron、任务调度程序、systemd)或部署到云功能。 CaptchaAI API 可用 24/7,正常运行时间超过 99.9%。
那些需要处理验证码之外的反机器人的机器人怎么办?
将 CaptchaAI 与隐形浏览器(未检测到的 chromedriver、puppeteer-extra)相结合。 CaptchaAI 处理验证码层;隐形工具处理特征检测。
相关指南
- 自动登录验证码处理
- 验证码自动化脚本
- 剧作家验证码处理