API 教程

如何自动解决网格图像验证码

网格图像验证码呈现分割成网格(通常为 3×3 或 4×4)的大图像,并要求用户选择与描述匹配的单元格。虽然 reCAPTCHA 使用这种格式,但许多网站使用不属于 Google 系统的自定义网格挑战

本指南涵盖使用 CaptchaAI 的 method=grid 端点解决非 reCAPTCHA 网格图像挑战。


要求

物品 价值
CaptchaAI API 密钥 验证码网站
网格图像 全网格的截图或base64
语言 Python 3.7+ 或 Node.js 14+

第 1 步:捕获网格图像

方法A:截图验证码元素

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://example.com/protected-form")

# Screenshot just the captcha container
captcha_element = driver.find_element(By.CSS_SELECTOR, "#captcha-container")
captcha_element.screenshot("captcha_grid.png")

方法B:从src属性中提取图像

import base64
import requests

captcha_img = driver.find_element(By.CSS_SELECTOR, ".grid-captcha img")
src = captcha_img.get_attribute("src")

if src.startswith("data:image"):
    image_b64 = src.split(",")[1]
else:
    image_data = requests.get(src).content
    image_b64 = base64.b64encode(image_data).decode()

第二步:将图片提交至CaptchaAI

使用文件上传(Python)

import requests
import time

API_KEY = "YOUR_API_KEY"

with open("captcha_grid.png", "rb") as f:
    response = requests.post("https://ocr.captchaai.com/in.php",
        data={
            "key": API_KEY,
            "method": "post",
            "recaptcha": 1,
            "json": 1
        },
        files={"file": f}
    )

data = response.json()
task_id = data["request"]
print(f"Task: {task_id}")

使用 base64 (Python)

response = requests.post("https://ocr.captchaai.com/in.php", data={
    "key": API_KEY,
    "method": "post",
    "body": image_b64,
    "recaptcha": 1,
    "json": 1
})

task_id = response.json()["request"]

Node.js

const axios = require('axios');
const fs = require('fs');

async function submitGridCaptcha(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: 'post',
      body: imageB64,
      recaptcha: 1,
      json: 1
    }
  });

  return data.request;
}

第 3 步:民意调查解决方案

def get_grid_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"]
        if result.get("request") != "CAPCHA_NOT_READY":
            raise Exception(f"Error: {result['request']}")

    raise Exception("Timeout")

solution = get_grid_solution(task_id)
print(f"Solution: {solution}")
# Returns click coordinates or cell indices

第 4 步:应用解决方案

按单元格索引单击

# If solution returns cell indices (e.g., "2,5,6")
selected = [int(i) for i in solution.split(",")]
cells = driver.find_elements(By.CSS_SELECTOR, ".grid-cell")

for idx in selected:
    cells[idx - 1].click()
    time.sleep(0.2)

driver.find_element(By.CSS_SELECTOR, ".verify-button").click()

按坐标点击

from selenium.webdriver.common.action_chains import ActionChains

# If solution returns coordinates (e.g., "x=120,y=80;x=250,y=200")
captcha_element = driver.find_element(By.CSS_SELECTOR, "#captcha-container")
actions = ActionChains(driver)

for coord in solution.split(";"):
    parts = dict(p.split("=") for p in coord.split(","))
    x, y = int(parts["x"]), int(parts["y"])
    actions.move_to_element_with_offset(captcha_element, x, y).click()

actions.perform()

故障排除

错误 原因 处理方式
ERROR_WRONG_FILE_EXTENSION 图像格式无效 使用 PNG 或 JPEG;验证base64是否有效
ERROR_CAPTCHA_UNSOLVABLE 图像太小或模糊 以全分辨率捕捉
选择了错误的单元格 解决方案格式不匹配 检查解决方案是否是索引与坐标
ERROR_TOO_BIG_CAPTCHA_FILESIZE 图片超出尺寸限制 调整大小至 600KB 以下

完整的可运行示例

需要一个包含环境设置、轮询、重试和错误处理的完整工作项目吗?

请参阅 GitHub 上的完整可运行示例 →


常问问题

我什么时候应该使用网格求解与令牌求解?

使用令牌解决 (method=userrecaptcha) 进行标准 reCAPTCHA 挑战 - 它更简单、更可靠。使用网格求解(method=postrecaptcha=1)来解决非 reCAPTCHA 网格挑战或独立图像网格。

支持哪些网格尺寸?

CaptchaAI 处理 3×3、4×4 和非标准网格布局。无论网格结构如何,图像都会被作为一个整体进行分析。

网格求解的精确度如何?

准确性取决于图像质量。高分辨率、清晰的图像可达到最佳效果。平均求解时间为 15-30 秒。

我可以解决图块变化的动态网格问题吗?

对于 reCAPTCHA 动态网格(其中单击的图块被替换),请使用令牌方法 (method=userrecaptcha)。网格法求解单个静态图像。


相关指南

该文章已禁用评论。