1.安装 Puppeteer 组件
⚠️在本博客中,将解释如何使用 Capsolver 插件设置 Puppeteer JS 来解决 reCaptcha v2。但您也可以将此方法应用于所有的captcha。
npm i puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
2.设置插件
下载插件的压缩包,并将其解压到项目根目录下的 ./CapSolver.Browser.Extension 文件夹中。
该插件具有许多设置,包括自动解决指定类型的captcha、支持代理等设置。这些设置可以在 ./assets/config.json 文件中找到。
这些设置包括:
{
"apiKey": "YourApiKey",
"useCapsolver": true, Ex: true / false
"useProxy": false, Ex: true / false
"proxyType": "http", Ex: http / socks5 / socks4
"hostOrIp": "", Ex: @google.com or 154.3.2.3
"port": "", Ex: 8983
"proxyLogin": "", Optional. Ex: dkkwje
"proxyPassword": "", Optional. Ex: dkwjek
"enabledForBlacklistControl": false, Ex: true / false
"blackUrlList": [], Ex: ["https://youtube.com","https://capsolver.com"] or [""https://capsolver.com"]
"enabledForRecaptcha": true, Ex: true / false
"enabledForRecaptchaV3": true, Ex: true / false
"enabledForHCaptcha": true, Ex: true / false
"enabledForFunCaptcha": true, Ex: true / false
"reCaptchaMode": "token", Ex: click / token
"hCaptchaMode": "click", Ex: click / token
"reCaptchaDelayTime": 0,
"hCaptchaDelayTime": 0,
"reCaptchaRepeatTimes": 10,
"reCaptcha3RepeatTimes": 10,
"hCaptchaRepeatTimes": 10,
"funCaptchaRepeatTimes": 10
}
在插件的设置文件 ./assets/config.json 中输入您的 API 密钥。您的密钥必须写入到 apiKey 字段的值中。您可以在页面上查看并复制您的 API 密钥。
示例:apiKey: "CAP-4FDBD3SDFSD-23S-2-3"
在此示例中,我们将使用 reCaptchaMode 的 token 模式,但我们也可以使用 click 模式。对于 reCaptcha,建议使用 token 模式。
3.在Puppeter上设置Capsolver插件以解决reCAPTCHA
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const { executablePath } = require('puppeteer');
(async () => {
const pathToExtension = require('path').join(__dirname, 'CapSolver.Browser.Extension');
puppeteer.use(StealthPlugin())
const browser = await puppeteer.launch({
headless: false,
args: [
`--disable-extensions-except=${pathToExtension}`,
`--load-extension=${pathToExtension}`,
],
executablePath: executablePath()
});
const [page] = await browser.pages()
})();
接下来,我们打开页面 https://www.google.com/recaptcha/api2/demo ↗,并将captcha发送给 Capsolver。
使用 page.goto() 方法,我们跳转到页面 https://www.google.com/recaptcha/api2/demo ↗。接下来,您需要发送captcha进行解决,可以手动或自动完成。
在我们的示例中,我们将手动发送验证码,为此我们等待插件按钮的 CSS 选择器 #recaptcha-anchor-label 可用,然后点击该按钮。点击按钮后,验证码将发送到服务进行解决。
await page.goto('https://www.google.com/recaptcha/api2/demo')
// Waiting for the element with the CSS selector ".captcha-solver" to be available
await page.waitForSelector('#recaptcha-anchor-label')
// Click on the element with the specified selector
await page.click('#recaptcha-anchor-label')
完整代码:
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const { executablePath } = require('puppeteer');
(async () => {
const pathToExtension = require('path').join(__dirname, 'CapSolver.Browser.Extension');
puppeteer.use(StealthPlugin())
const browser = await puppeteer.launch({
headless: false,
args: [
`--disable-extensions-except=${pathToExtension}`,
`--load-extension=${pathToExtension}`,
],
executablePath: executablePath()
});
const [page] = await browser.pages()
// Opening a page
await page.goto('https://www.google.com/recaptcha/api2/demo')
// Waiting for the element with the CSS selector "#recaptcha-anchor-label" to be available
await page.waitForSelector('#recaptcha-anchor-label')
// Click on the element with the specified selector
await page.click('#recaptcha-anchor-label')
})();
就是这样!您使用 Capsolver插件和 Puppeteer 成功解决了 reCaptcha v2。如果您需要解决其他类型的验证码,只需按照相应的教程进行复制即可。