close menu
सपोर्टेड टास्क टाइप
API विधियाँ
आर्टिकल
GitHub icon
GitHub
Menu

Puppeteer या Selenium में एंटी-कैप्चा प्लगइन का इस्तेमाल कैसे करें

Puppeteer और Selenium ब्राउज़र ऑटोमेशन के दो प्रमुख इंजन हैं व हमारा प्लगइन बड़ी आसानी से उनमें इंटीग्रेट हो जाता है। इस आर्टिकल में हम आपको दिखाएंगे कि NodeJS और Python प्रोग्रामिंग लैंग्वेजों के लिए क्रमशः Puppeteer और Selenium में उसका इस्तेमाल कैसे किया जाता है। अगर आप इन दोनों में से किसी एक का चयन करने वाले हैं, तो अपने मूल एनवायरनमेंट के चलते हम आपको NodeJS+Puppeteer का इस्तेमाल करने की ही सलाह देंगे।

1. डिपेंडेंसियों को इनस्टॉल कर लें। NodeJS के लिए केवल नीचे दिए npm पैकेजों को ही इनस्टॉल कर लें, Python के लिए पैकेजों को इनस्टॉल करके इस पेज से एक्सीक्यूट किए जा सकने वाले "chromedriver" को डाउनलोड कर लें। इस ड्राइवर का वर्शन आपके सिस्टम में इनस्टॉल किए गए Chrome वर्शन से मेल खाना चाहिए।

Javascript
Python
npm install adm-zip puppeteer puppeteer-extra puppeteer-extra-plugin-stealth

2. प्लगइन को Chrome के ZIP वर्शन में डाउनलोड कर लें और फिर उसे अपने प्रोजेक्ट फोल्डर में अनज़िप कर लें। वास्तविक वर्शन यहाँ मौजूद होते हैं। आप ऐसा प्रोग्राम के माध्यम से भी कर सकते हैं:

Javascript
Python
//npm install adm-zip
const https = require('https')
const fs = require('fs');
const AdmZip = require("adm-zip");

const pluginURL = 'https://antcpt.com/anticaptcha-plugin.zip';

(async () => {
    // प्लगइन को डाउनलोड कर लें
    await new Promise((resolve) => {
        https.get(pluginURL, resp => resp.pipe(fs.createWriteStream('./plugin.zip').on('close', resolve)));
    })
    // अनज़िप करें
    const zip = new AdmZip("./plugin.zip");
    await zip.extractAllTo("./plugin/", true);
})();

3. इसके बाद अपनी API कुंजी को ./plugin/js/config_ac_api_key.js फाइल में कॉन्फ़िगर कर लें। अपनी API कुंजी को आप ग्राहक एरिया में देख सकते हैं। इसे चलाने के लिए आपको थोड़े-बहुत पॉज़िटिव बैलेंस की ज़रूरत होगी।

Javascript
Python
const apiKey = 'API_KEY_32_BYTES';
if (fs.existsSync('./plugin/js/config_ac_api_key.js')) {
    let confData = fs.readFileSync('./plugin/js/config_ac_api_key.js', 'utf8');
    confData = confData.replace(/antiCapthaPredefinedApiKey = ''/g, `antiCapthaPredefinedApiKey = '${apiKey}'`);
    fs.writeFileSync('./plugin/js/config_ac_api_key.js', confData, 'utf8');
} else {
    console.error('plugin configuration not found!')
}

4. ब्राउज़र को प्लगइन के माध्यम से शुरू कर लें। Puppeteer के मामले में हम 'puppeteer-extra' पैकेज वाले 'puppeteer-extra-plugin-stealth' प्लगइन के इस्तेमाल का सुझाव देते हैं। यह प्लगइन वेबसाइट द्वारा स्वचालित Chromium ब्राउज़र की सभी निशानियों को छिपा देता है।

Javascript
Python
//npm install puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());

(async () => {
    const browser = await puppeteer.launch({
        headless: false,
        ignoreDefaultArgs: [
            "--disable-extensions",
            "--enable-automation"
        ],
        args: [
            '--disable-web-security',
            '--disable-features=IsolateOrigins,site-per-process',
            '--allow-running-insecure-content',
            '--disable-blink-features=AutomationControlled',
            '--no-sandbox',
            '--mute-audio',
            '--no-zygote',
            '--no-xshm',
            '--window-size=1920,1080',
            '--no-first-run',
            '--no-default-browser-check',
            '--disable-dev-shm-usage',
            '--disable-gpu',
            '--enable-webgl',
            '--ignore-certificate-errors',
            '--lang=en-US,en;q=0.9',
            '--password-store=basic',
            '--disable-gpu-sandbox',
            '--disable-software-rasterizer',
            '--disable-background-timer-throttling',
            '--disable-backgrounding-occluded-windows',
            '--disable-renderer-backgrounding',
            '--disable-infobars',
            '--disable-breakpad',
            '--disable-canvas-aa',
            '--disable-2d-canvas-clip-aa',
            '--disable-gl-drawing-for-tests',
            '--enable-low-end-device-mode',
            '--disable-extensions-except=./plugin',
            '--load-extension=./plugin'
        ]
    });
    const page = await browser.newPage();
})();

5. लक्षित पेज पर जाकर ज़रूरत पड़ने पर एक फॉर्म भर दें। प्लगइन अपने आप ही reCAPTCHA को उठाकर उसे हल करना शुरू कर देगा।

Javascript
Python
(async () => {
    const url = 'https://anti-captcha.com/demo/?page=recaptcha_v2_textarea';
    const login = 'Test login';
    const password = 'Test password';

    try {
        await page.goto(url, {
            waitUntil: "networkidle0"
        });
    } catch (e) {
        console.error('err while loading the page: '+e);
    }
    // नेविगेशन टाइमआउट एरर्स को डिसएबल कर दें
    await page.setDefaultNavigationTimeout(0);

    await page.$eval('#login', (element, login) => {
        element.value = login;
    }, login);
    await page.$eval('#password', (element, password) => {
        element.value = password;
    }, password);

})();

6. अगला चरण थोड़ा पेचीदा है। कुछ वेब फॉर्मों में उपयोगकर्ता को reCAPTCHA हल करने के बाद सबमिट वाला बटन दबाना होता है, जबकि अन्य वेब फॉर्म कॉलबैक का इस्तेमाल करके उन्हें अपने आप ही सबमिट कर देते हैं। पहली स्थिति में हमें reCAPTCHA के हल होते ही सबमिट वाला बटन दबा देना होता है। सही समय पर बटन दबाने के लिए बस .antigate_solver.solved सिलेक्टर के दिखाई देने का इंतज़ार करें और उसके आते ही सबमिट वाला बटन दबा दें।

Javascript
Python
// "हल किए गए" सिलेक्टर के दिखाई देने का इंतज़ार करें
await page.waitForSelector('.antigate_solver.solved').catch(error => console.log('failed to wait for the selector'));
console.log('{{ $t('articles.how-to-integrate.code-comments.recaptcha-solved') }}');

// सबमिट वाला बटन दबा दें
await Promise.all([
    page.click('#submitButton'),
    page.waitForNavigation({ waitUntil: "networkidle0" })
]);
console.log('टास्क पूरी हुई, reCAPTCHA वाला फॉर्म बाईपास हुआ');

बस इतना ही। फॉर्म भरा जा चुका है व reCAPTCHA हल होकर बाईपास किया जा चुका है। पूरे कोड के सैंपल:

Javascript
Python
// first run the following to install required npm packages:
//
// npm install adm-zip follow-redirects puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
//
//
const https = require('follow-redirects').https;
const fs = require('fs');
const AdmZip = require("adm-zip");

const apiKey = 'YOUR_API_KEY_HERE!';
const pluginURL = 'https://antcpt.com/anticaptcha-plugin.zip';
const url = 'https://anti-captcha.com/demo/?page=recaptcha_v2_textarea';
const login = 'Test login';
const password = 'Test password';
let page = null;


const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());

(async () => {
    // प्लगइन को डाउनलोड कर लें
    await new Promise((resolve) => {
        https.get(pluginURL, resp => resp.pipe(fs.createWriteStream('./plugin.zip').on('close', resolve)));
    })
    // अनज़िप करें
    const zip = new AdmZip("./plugin.zip");
    await zip.extractAllTo("./plugin/", true);

    // कॉन्फ़िगरेशन फाइल में जाकर API कुंजी सेट कर दें
    await new Promise((resolve, reject) => {
        if (fs.existsSync('./plugin/js/config_ac_api_key.js')) {
            let confData = fs.readFileSync('./plugin/js/config_ac_api_key.js', 'utf8');
            confData = confData.replace(/antiCapthaPredefinedApiKey = ''/g, `antiCapthaPredefinedApiKey = '${apiKey}'`);
            fs.writeFileSync('./plugin/js/config_ac_api_key.js', confData, 'utf8');
            resolve();
        } else {
            console.error('plugin configuration not found!')
            reject();
        }
    });

    // ब्राउज़र लॉन्च विकल्प सेट कर दें
    const options = {
        headless: false,
        ignoreDefaultArgs: [
            "--disable-extensions",
            "--enable-automation"
        ],
        args: [
            '--disable-web-security',
            '--disable-features=IsolateOrigins,site-per-process',
            '--allow-running-insecure-content',
            '--disable-blink-features=AutomationControlled',
            '--no-sandbox',
            '--mute-audio',
            '--no-zygote',
            '--no-xshm',
            '--window-size=1920,1080',
            '--no-first-run',
            '--no-default-browser-check',
            '--disable-dev-shm-usage',
            '--disable-gpu',
            '--enable-webgl',
            '--ignore-certificate-errors',
            '--lang=en-US,en;q=0.9',
            '--password-store=basic',
            '--disable-gpu-sandbox',
            '--disable-software-rasterizer',
            '--disable-background-timer-throttling',
            '--disable-backgrounding-occluded-windows',
            '--disable-renderer-backgrounding',
            '--disable-infobars',
            '--disable-breakpad',
            '--disable-canvas-aa',
            '--disable-2d-canvas-clip-aa',
            '--disable-gl-drawing-for-tests',
            '--enable-low-end-device-mode',
            '--disable-extensions-except=./plugin',
            '--load-extension=./plugin'
        ]
    }

    try {
        // ब्राउज़र को प्लगइन के माध्यम से लॉन्च कर दें
        const browser = await puppeteer.launch();
        page = await browser.newPage();
    } catch (e) {
        console.log('could not launch browser: '+e.toString())
        return;
    }

    // लक्षित पेज पर चले जाएँ
    try {
        await page.goto(url, {
            waitUntil: "networkidle0"
        });
    } catch (e) {
        console.error('err while loading the page: '+e);
    }

    // नेविगेशन टाइमआउट एरर्स को डिसएबल कर दें
    await page.setDefaultNavigationTimeout(0);

    // फॉर्म भर दें
    await page.$eval('#login', (element, login) => {
        element.value = login;
    }, login);
    await page.$eval('#password', (element, password) => {
        element.value = password;
    }, password);

    // "हल किए गए" सिलेक्टर के दिखाई देने का इंतज़ार करें
    await page.waitForSelector('.antigate_solver.solved').catch(error => console.log('failed to wait for the selector'));
    console.log('{{ $t('articles.how-to-integrate.code-comments.recaptcha-solved') }}');

    // सबमिट वाला बटन दबा दें
    await Promise.all([
        page.click('#submitButton'),
        page.waitForNavigation({ waitUntil: "networkidle0" })
    ]);
    console.log('reCAPTCHA हल किया गया');

})();

बोनस: चूंकि Chrome, प्लगइन्स के माध्यम से ब्राउज़र ऑटोमेशन को सपोर्ट नहीं करता, प्लगइन को किसी हैडलेस मोड में चलाने की एक तरकीब होती है। ऐसे में, आप अपनी एप्लीकेशन के लिए वर्चुअल डेस्कटॉप मुहैया कराने वाले Xvfb फीचर का उपयोग कर सकते हैं।

# पैकेज को इनस्टॉल कर लें
apt-get install -y xvfb

# डिस्प्ले वेरिएबल को निर्दिष्ट कर दें
export DISPLAY=:0

# बैकग्राउंड में Xvfb daemon शुरू कर दें (सिर्फ़ एक बार)
/usr/bin/Xvfb :0 -screen 0 1024x768x24 &

# उसके दिखाई देने तक थोड़ा इंतज़ार करें (सिर्फ़ एक बार)
sleep 5

# "node" या "python" स्क्रिप्ट में "xvfb-run" प्रीफिक्स जोड़ दें
xvfb-run node myscript.js
# या फ़िर
xvfb-run python myscript.py