Join And Get Free Trial!

使用几行代码将验证码解决添加到您的自动化中#

logoNextCaptcha
March 29,2024

使用NextCaptcha自动解决验证码#

如果您从事网络抓取等浏览器自动化任务,您可能会遇到过通过验证码挑战中断您的脚本的网站。

让NextCaptcha处理您的验证码解决问题#

如果您的 Puppeteer 或 Playwright 脚本被顽固的机器人检测器阻止,您将需要检查这一点。通过一个简短的代码片段,它使您的自动化能够解决任何验证码。

代码片段

const puppeteer= require('puppeteer');
const axios = require('axios');
const url = 'https://www.google.com/recaptcha/api2/demo';
 
async function main() {
    const browser = await puppeteer.launch({
        headless: false
    });
 
    const page = await browser.newPage();
 
    await page.goto(url, {
        waitUntil: 'networkidle0',
    });
 
    const sitekey = await page.$eval('[data-sitekey]', el => el.getAttribute('data-sitekey'));
 
    if (sitekey) {
        const task = await createCaptchaTask(url, sitekey);
        if (task) {
            const result = await getTaskResult(task.taskId);
            if (!result.errorId) {
                await page.evaluate((value) => {
 
                    const textarea = document.querySelector('#g-recaptcha-response');
                    textarea.value = value;
                }, result.solution.gRecaptchaResponse);
                await page.click('#recaptcha-demo-submit');
 
				        // Continue...
            }
        }
 
    } else {
        console.log("find sitekey error");
    }
 
};
 
async function createCaptchaTask(url, siteKey, isInvisible = false) {
    try {
        const data = await axios.post('https://api.nextcaptcha.com/createTask', {
            "clientKey": "CLIENT_KEY", // clientKey from NextCaptcha
            "task": {
                type: "RecaptchaV2TaskProxyless",
                websiteURL: url,
                websiteKey: siteKey,
                isInvisible
            }
        });
        return data.data;
    } catch (e) {
        console.error('createCaptchaTask error', e);
        return null;
    }
}
 
async function sleep(time = 500) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve();
        }, time)
    })
}
//
async function getTaskResult(taskId, tryTimes = 60 ) {
    try {
        console.log('tryTimes', tryTimes)
        const data = await axios.post('https://api.nextcaptcha.com/getTaskResult', {
            "clientKey": "CLIENT_KEY", // clientKey from NextCaptcha
            taskId
        });
				if (data.data.status === 'processing' && tryTimes >= 0) {
            await sleep();
            return getTaskResult(taskId, tryTimes-1);
        } else if (data.data.status === 'ready') {
            return data.data;
        } else {
            if (tryTimes < 0) {
                console.error('getTaskResult out of time');
            } else {
                console.error('getTaskResult errorCode', data.data.errorCode);
                console.error('getTaskResult errorDescription', data.data.errorDescription);
                return null;
            }
        }
    } catch (e) {
        console.error('createCaptchaTask error', e.message);
        return null;
    }
}
 
main();
您的脚本不需要做任何额外的工作,所需要做的就是等待NextCaptcha使用我们的求解器 API 求解 我们还有适用于 Python、Go、C# 的验证码求解器 sdk。我们将集成更多的编程语言

captcha solver Python sdk: nextcaptcha-python

captcha solver Go sdk: nextcaptcha-go

captcha solver C# sdk: nextcaptcha-csharp


联系我们

Email: [email protected]

Telegram: https://t.me/+wWJh5iON_I0xY2Vh

Website: https://nextcaptcha.com/