Hướng Dẫn API

Giải CAPTCHA hình ảnh bằng Node.js và CaptchaAI

CAPTCHA hình ảnh hiển thị văn bản bị bóp méo mà người dùng phải nhập. Chúng xuất hiện trên các trang web của chính phủ, biểu mẫu cũ và trang đăng ký.CaptchaAIđọc hình ảnh và trả về văn bản. Hướng dẫn này chỉ ra cách thực hiện điều đó từ Node.js.


Điều kiện tiên quyết

Mục Giá trị
Khóa API CaptchaAI Từcaptchaai.com
Node.js 14+
Thư viện axios, fs
Định dạng hình ảnh JPG, PNG hoặc GIF (100 byte – 100 KB)

Phương pháp A: Gửi 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}`);

Phương pháp B: Tải tệp lên

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;

Thăm dò kết quả văn bản

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);
}

Thông số chính xác

// 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,
  },
});
tham số Giá trị Mục đích
numeric 1 = chữ số, 2 = chữ cái Giới hạn ký tự
min_len / max_len số nguyên Hạn chế về độ dài
calc 1 Tính biểu thức toán học
regsense 1 Phân biệt chữ hoa chữ thường

Hoàn thành ví dụ làm việc

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);

Sản lượng dự kiến:

Solved: ABC123

Các lỗi thường gặp

Lỗi nguyên nhân sửa chữa
ERROR_WRONG_FILE_EXTENSION Định dạng không được hỗ trợ Sử dụng JPG, PNG hoặc GIF
ERROR_TOO_BIG_CAPTCHA_FILESIZE Hình ảnh > 100 KB Nén trước
ERROR_ZERO_CAPTCHA_FILESIZE Hình ảnh < 100 byte Xác minh hình ảnh
CAPCHA_NOT_READY Vẫn đang giải quyết Thăm dò ý kiến cứ sau 5 giây

Câu hỏi thường gặp

Tôi có thể giải CAPTCHA toán học không?

Vâng. Thêm calc: 1 vào tham số và CaptchaAI sẽ trả về kết quả tính toán.

Làm cách nào để báo cáo các giải pháp sai?

Gọi https://ocr.captchaai.com/res.php?key=KEY&action=reportbad&id=TASK_ID để báo kết quả sai.

Base64 hoặc tải tệp lên nhanh hơn?

Hiệu suất là như nhau. Base64 thuận tiện hơn khi bạn đã có sẵn hình ảnh trong bộ nhớ.


Hướng dẫn liên quan


Bắt đầu giải CAPTCHA hình ảnh bằng CaptchaAI →

Os comentários estão desativados para este artigo.