Gambar CAPTCHA menampilkan teks terdistorsi yang harus diketik pengguna. Mereka muncul di situs pemerintah, formulir warisan, dan halaman pendaftaran.CaptchaAImembaca gambar dan mengembalikan teks. Panduan ini menunjukkan cara melakukannya dari Node.js.
Prasyarat
| Barang | Nilai |
|---|---|
| Kunci API CaptchaAI | Daricaptchaai.com |
| Node.js | 14+ |
| Perpustakaan | axios, fs |
| Format gambar | JPG, PNG, atau GIF (100 byte – 100 KB) |
Metode A: Pengiriman 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}`);
Metode B: Unggah file
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;
Jajak pendapat untuk hasil teks
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);
}
Parameter akurasi
// 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,
},
});
| Parameter | Nilai | Tujuan |
|---|---|---|
numeric |
1 = angka, 2 = huruf |
Membatasi karakter |
min_len / max_len |
bilangan bulat | Batasan panjang |
calc |
1 |
Menghitung ekspresi matematika |
regsense |
1 |
Peka huruf besar-kecil |
Contoh kerja lengkap
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);
Hasil yang diharapkan:
Solved: ABC123
Kesalahan umum
| Kesalahan | Penyebab | Solusi |
|---|---|---|
ERROR_WRONG_FILE_EXTENSION |
Formatnya tidak didukung | Gunakan JPG, PNG, atau GIF |
ERROR_TOO_BIG_CAPTCHA_FILESIZE |
Gambar > 100 KB | Kompres terlebih dahulu |
ERROR_ZERO_CAPTCHA_FILESIZE |
Gambar <100 byte | Verifikasi gambarnya |
CAPCHA_NOT_READY |
Masih menyelesaikan | Jajak pendapat setiap 5 detik |
Pertanyaan Umum
Bisakah saya menyelesaikan CAPTCHA matematika?
Ya. Tambahkan calc: 1 ke parameter dan CaptchaAI akan mengembalikan hasil yang dihitung.
Bagaimana cara melaporkan solusi yang salah?
Hubungi https://ocr.captchaai.com/res.php?key=KEY&action=reportbad&id=TASK_ID untuk melaporkan hasil yang salah.
Apakah upload base64 atau file lebih cepat?
Performanya sama. Base64 lebih nyaman bila Anda sudah memiliki gambar di memori.