图像验证码(也称为普通验证码或文本验证码)显示用户必须键入的扭曲文本。尽管它是最古老的验证码格式,但它们仍然在许多遗留系统、政府门户和注册表中使用。
CaptchaAI 通过接受图像并返回识别的文本来解决这些问题。
要求
| 物品 | 价值 |
|---|---|
| CaptchaAI API 密钥 | 从验证码网站 |
| 验证码图像 | 文件或base64 |
| 语言 | Python 3.7+ 或 Node.js 14+ |
第 1 步:捕获验证码图像
截图方法(Selenium)
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com/register")
captcha_el = driver.find_element(By.CSS_SELECTOR, "#captcha-image")
captcha_el.screenshot("captcha.png")
从网址下载
import requests
import base64
img_url = "https://example.com/captcha/generate"
img_data = requests.get(img_url).content
# Save to file
with open("captcha.png", "wb") as f:
f.write(img_data)
# Or convert to base64
img_b64 = base64.b64encode(img_data).decode()
第二步:提交至CaptchaAI
方法A:文件上传(Python)
import requests
import time
API_KEY = "YOUR_API_KEY"
with open("captcha.png", "rb") as f:
response = requests.post("https://ocr.captchaai.com/in.php",
data={"key": API_KEY, "method": "post", "json": 1},
files={"file": ("captcha.png", f, "image/png")}
)
data = response.json()
task_id = data["request"]
print(f"Task: {task_id}")
方法 B:Base64 (Python)
response = requests.post("https://ocr.captchaai.com/in.php", data={
"key": API_KEY,
"method": "base64",
"body": img_b64,
"json": 1
})
task_id = response.json()["request"]
Node.js (base64)
const axios = require('axios');
const fs = require('fs');
async function submitImageCaptcha(imagePath) {
const imageB64 = fs.readFileSync(imagePath).toString('base64');
const { data } = await axios.post('https://ocr.captchaai.com/in.php', null, {
params: {
key: 'YOUR_API_KEY',
method: 'base64',
body: imageB64,
json: 1
}
});
return data.request;
}
步骤 3:轮询文本结果
def get_text_solution(task_id):
for _ in range(30):
time.sleep(5)
result = requests.get("https://ocr.captchaai.com/res.php", params={
"key": API_KEY, "action": "get", "id": task_id, "json": 1
}).json()
if result.get("status") == 1:
return result["request"] # The recognized text
if result.get("request") != "CAPCHA_NOT_READY":
raise Exception(f"Error: {result['request']}")
raise Exception("Timeout")
text = get_text_solution(task_id)
print(f"CAPTCHA text: {text}")
async function getSolution(taskId) {
for (let i = 0; i < 30; i++) {
await new Promise(r => setTimeout(r, 5000));
const { data } = await axios.get('https://ocr.captchaai.com/res.php', {
params: { key: 'YOUR_API_KEY', action: 'get', id: taskId, json: 1 }
});
if (data.status === 1) return data.request;
if (data.request !== 'CAPCHA_NOT_READY') throw new Error(data.request);
}
throw new Error('Timeout');
}
第 4 步:将文本提交到表单
# Type the solved text into the CAPTCHA input
captcha_input = driver.find_element(By.CSS_SELECTOR, "#captcha-input")
captcha_input.clear()
captcha_input.send_keys(text)
# Submit the form
driver.find_element(By.CSS_SELECTOR, "form").submit()
可选参数以获得更好的精度
| 范围 | 价值 | 目的 |
|---|---|---|
numeric |
1 = 仅数字,2 = 仅字母 |
限制字符集 |
min_len |
整数 | 最小文本长度 |
max_len |
整数 | 最大文本长度 |
language |
0 = 任意、1 = 西里尔字母、2 = 拉丁字母 |
人物语言 |
calc |
1 |
CAPTCHA 是一个数学表达式 |
phrase |
1 |
验证码包含空格 |
regsense |
1 |
区分大小写 |
response = requests.post("https://ocr.captchaai.com/in.php", data={
"key": API_KEY,
"method": "base64",
"body": img_b64,
"numeric": 1, # Digits only
"min_len": 4, # At least 4 characters
"max_len": 6, # At most 6 characters
"json": 1
})
完整的 Python 示例
import requests
import time
import base64
from selenium import webdriver
from selenium.webdriver.common.by import By
API_KEY = "YOUR_API_KEY"
# 1. Get the page and capture captcha
driver = webdriver.Chrome()
driver.get("https://example.com/register")
captcha_el = driver.find_element(By.CSS_SELECTOR, "#captcha-image")
captcha_el.screenshot("captcha.png")
# 2. Submit to CaptchaAI
with open("captcha.png", "rb") as f:
img_b64 = base64.b64encode(f.read()).decode()
resp = requests.post("https://ocr.captchaai.com/in.php", data={
"key": API_KEY, "method": "base64", "body": img_b64, "json": 1
}).json()
task_id = resp["request"]
# 3. Get solution
for _ in range(30):
time.sleep(5)
result = requests.get("https://ocr.captchaai.com/res.php", params={
"key": API_KEY, "action": "get", "id": task_id, "json": 1
}).json()
if result.get("status") == 1:
text = result["request"]
break
# 4. Type and submit
driver.find_element(By.CSS_SELECTOR, "#captcha-input").send_keys(text)
driver.find_element(By.CSS_SELECTOR, "form").submit()
print(f"Solved: {text}")
driver.quit()
完整的可运行示例
需要一个包含环境设置、轮询、重试和错误处理的完整工作项目吗?
常问问题
图像验证码解析的准确度如何?
CaptchaAI 实现了标准文本验证码的高精度。使用提示参数(numeric、min_len、max_len)通过约束字符集来提高准确性。
图像验证码的求解速度有多快?
通常为 5-15 秒,比网格或交互式挑战更快。
我可以解决数学验证码吗?
是的。设置 calc=1,求解器将计算结果(例如,“3 + 7”返回“10”)。
如果验证码文本区分大小写怎么办?
设置 regsense=1 以保留字母大小写。如果没有这个,求解器可能会返回小写。
我可以报告错误的解决方案吗?
是的。使用 https://ocr.captchaai.com/res.php?key=KEY&action=reportbad&id=TASK_ID 报告错误的解决方案。这有助于提高准确性并可能退还解决成本。
相关指南
- 图像验证码解析 (OCR) 的工作原理
- 常见 OCR 验证码错误和修复
- 什么是解决媒体验证码