close menu
সমর্থিত টাস্ক ধরণ
API পদ্ধতিসমূহ
নিবন্ধ
GitHub icon
GitHub
Menu

Puppeteer বা Selenium -এ এন্টি-ক্যাপচা প্লাগইন কিভাবে ব্যবহার করবেন

Puppeteer এবং Selenium ব্রাউজার অটোমেশনের জন্য দুটি প্রধান ইঞ্জিন এবং আমাদের প্লাগইন তাদের মধ্যে অবিচ্ছিন্নভাবে একীভূত হয়। এই প্রবন্ধে আমরা দেখাবো কিভাবে এটি যথাক্রমে NodeJS এবং Python প্রোগ্রামিং ভাষার জন্য Puppeteer এবং Selenium-এ ব্যবহার করতে হয়। আপনি যদি দুটির মধ্যে বেছে নেন, আমরা এটির স্থানীয় পরিবেশের কারনে NodeJS+Puppeteer দৃঢ়ভাবে সুপারিশ করে থাকি।

১। ডিপেন্ডেন্সি ইনস্টল করুন। এর ​​জন্য শুধু নীচের দেওয়া npm প্যাকেজ ইনস্টল করুন, Python-এর জন্য প্যাকেজ ইনস্টল করুন এবং এই পেজ থেকে "chromedriver" এক্সিকিউটেবল ডাউনলোড করুন। ড্রাইভার সংস্করণটি আপনার সিস্টেমে ইনস্টল করা Chrome সংস্করণের সাথে মেলার কথা।

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

২। ক্রোমের জন্য জিপ সংস্করণে প্লাগইন ডাউনলোড করুন, এটি আপনার প্রোজেক্ট ফোল্ডারে আনজিপ করুন। প্রকৃত সংস্করণগুলি অবস্থিত এখানে। আপনি এটি প্রোগ্রামগতভাবেও করতে পারেনঃ

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

৩। এরপর, ./plugin/js/config_ac_api_key.js ফাইলে আপনার API কী কনফিগার করুন। আপনি গ্রাহক এরিয়া -এ আপনার 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!')
}

৪। প্লাগইন দিয়ে ব্রাউজার শুরু করুন। 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();
})();

৫। একটি টার্গেট পেজে নেভিগেট করুন এবং প্রয়োজন হলে একটি ফর্ম পূরণ করুন। প্লাগইনটি স্বয়ংক্রিয়ভাবে রিক্যাপচা ধরে ফেলবে এবং এটি সমাধান করা শুরু করবে।

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

})();

৬। পরবর্তীটি একটু জটিল অংশ। কিছু ওয়েব ফর্ম ব্যবহারকারীদের 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 সমাধান এবং বাইপাস করা হয়েছে। সম্পূর্ণ কোড নমুনাঃ

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