使用插件模拟人流量
import NextCaptcha from 'nextcaptcha-ts';
import puppeteer from 'puppeteer';
然后,打开 Chrome 实例并导航到演示页面。
const url = "https://www.google.com/recaptcha/api2/demo"
const browser = await puppeteer.launch({});
const page = await browser.newPage();
await page.goto(url, {
waitUntil: 'networkidle0',
});
下一步是找到验证码 websiteKey 和 websiteURL
NextCaptcha客户端,返回文本解决方案。将其保存在 data.solution.gRecaptchaResponse 变量中。
const data = await client.recaptchaV2({websiteKey, websiteURL: url});
console.log('gRecaptchaResponse: ', data.solution.gRecaptchaResponse);
console.log('submitting form .. ');
await Promise.all([
page.click('#recaptcha-demo-submit'),
page.waitForNavigation({ waitUntil: "networkidle0" })
]);
console.log('making a screenshot ...');
await page.screenshot({ path: 'screenshot.png' });
console.log('closing browser .. ');
await browser.close();
import NextCaptcha from 'nextcaptcha-ts';
import puppeteer from 'puppeteer';
const url = "https://bot.sannysoft.com"
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
await page.goto(url, {
waitUntil: 'networkidle0',
});
console.log('making a screenshot ...');
await page.screenshot({ path: 'screenshot.png' });
await sleep(30000)
console.log('closing browser .. ');
await browser.close();
function sleep(time = 1000) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true)
}, time)
})
}
它将显示 WebDrive 存在(失败)
拒绝访问!sannysoft 检测到非人类流量并阻止了我们的机器人。这时,puppeteer-extra-plugin-stealth(一个用于 puppeteer-extra 和 playwright-extra 的插件,用于防止检测)可以派上用场。它将使您的流量看起来更多的手动,并防止被阻止,即使用 CAPTCHA。
首先,安装 Stealth 包
import puppeteer from 'puppeteer-extra';
import puppeteer-extra-plugin-stealth from 'puppeteer-extra-plugin-stealth';
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
然后像平常一样使用木偶
const url = "https://bot.sannysoft.com"
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
await page.goto(url, {
waitUntil: 'networkidle0',
});
console.log('making a screenshot ...');
await page.screenshot({ path: 'screenshot.png' });
await sleep(30000)
console.log('closing browser .. ');
await browser.close();
function sleep(time = 1000) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true)
}, time)
})
}
让我们看看输出:
成功!这有助于使用 Puppeteer 绕过验证码 。