API 教程

使用 Node.js 和 CaptchaAI 解决图像验证码问题

图像验证码显示用户必须输入的扭曲文本。它们出现在政府网站、遗留表格和注册页面上。CaptchaAI读取图像并返回文本。本指南展示了如何从 Node.js 执行此操作。


先决条件

物品 价值
CaptchaAI API 密钥 验证码网站
Node.js 14+
图书馆 axiosfs
图片格式 JPG、PNG 或 GIF(100 字节 – 100 KB)

方法A:Base64提交

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

const API_KEY = 'YOUR_API_KEY';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

// Read and encode the image
const imageB64 = fs.readFileSync('captcha.png').toString('base64');

// Submit to CaptchaAI
const { data: submitData } = await axios.post('https://ocr.captchaai.com/in.php', null, {
  params: {
    key: API_KEY,
    method: 'base64',
    body: imageB64,
    json: 1,
  },
});

if (submitData.status !== 1) throw new Error(submitData.request);
const taskId = submitData.request;
console.log(`Task submitted: ${taskId}`);

方法B:文件上传

const FormData = require('form-data');

const form = new FormData();
form.append('key', API_KEY);
form.append('method', 'post');
form.append('json', '1');
form.append('file', fs.createReadStream('captcha.png'));

const { data: submitData } = await axios.post('https://ocr.captchaai.com/in.php', form, {
  headers: form.getHeaders(),
});

const taskId = submitData.request;

投票文本结果

await sleep(5000);

let captchaText;
for (let i = 0; i < 30; i++) {
  const { data: pollData } = await axios.get('https://ocr.captchaai.com/res.php', {
    params: { key: API_KEY, action: 'get', id: taskId, json: 1 },
  });

  if (pollData.status === 1) {
    captchaText = pollData.request;
    console.log(`CAPTCHA text: ${captchaText}`);
    break;
  }
  if (pollData.request !== 'CAPCHA_NOT_READY') {
    throw new Error(pollData.request);
  }
  await sleep(5000);
}

精度参数

// Digits only, 4-6 characters
const { data } = await axios.post('https://ocr.captchaai.com/in.php', null, {
  params: {
    key: API_KEY,
    method: 'base64',
    body: imageB64,
    numeric: 1,      // digits only
    min_len: 4,       // minimum length
    max_len: 6,       // maximum length
    json: 1,
  },
});
范围 价值 目的
numeric 1 = 数字,2 = 字母 限制字符数
min_len / max_len 整数 长度限制
calc 1 计算数学表达式
regsense 1 区分大小写

完整的工作示例

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

const API_KEY = 'YOUR_API_KEY';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

async function solveImageCaptcha() {
  // 1. Load page and screenshot CAPTCHA
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com/register');

  const captchaEl = await page.$('#captcha-image');
  await captchaEl.screenshot({ path: 'captcha.png' });

  // 2. Encode and submit
  const imageB64 = fs.readFileSync('captcha.png').toString('base64');
  const { data: submit } = await axios.post('https://ocr.captchaai.com/in.php', null, {
    params: { key: API_KEY, method: 'base64', body: imageB64, json: 1 },
  });
  const taskId = submit.request;

  // 3. Poll for text
  await sleep(5000);
  let text;
  for (let i = 0; i < 30; i++) {
    const { data: poll } = await axios.get('https://ocr.captchaai.com/res.php', {
      params: { key: API_KEY, action: 'get', id: taskId, json: 1 },
    });
    if (poll.status === 1) { text = poll.request; break; }
    if (poll.request !== 'CAPCHA_NOT_READY') throw new Error(poll.request);
    await sleep(5000);
  }

  // 4. Type and submit
  await page.type('#captcha-input', text);
  await page.click('form [type="submit"]');
  console.log(`Solved: ${text}`);
  await browser.close();
}

solveImageCaptcha().catch(console.error);

预期输出:

Solved: ABC123

常见错误

错误 原因 处理方式
ERROR_WRONG_FILE_EXTENSION 不支持的格式 使用 JPG、PNG 或 GIF
ERROR_TOO_BIG_CAPTCHA_FILESIZE 图片 > 100 KB 先压缩
ERROR_ZERO_CAPTCHA_FILESIZE 图像 < 100 字节 验证图像
CAPCHA_NOT_READY 仍在解决 每 5 秒轮询一次

常问问题

我可以解决数学验证码吗?

是的。参数中添加calc: 1,CaptchaAI将返回计算结果。

如何报告错误的解决方案?

致电 https://ocr.captchaai.com/res.php?key=KEY&action=reportbad&id=TASK_ID 报告错误结果。

是base64还是文件上传更快?

性能是一样的。当内存中已有图像时,Base64 会更方便。


相关指南


开始使用 CaptchaAI → 解决图像验证码

该文章已禁用评论。