如何使用 Puppeteer 和 Capsolver 扩展程序解决 reCaptcha
1. 安装 Puppeteer 组件
在这篇博客中,我们将解释如何使用 Capsolver 扩展程序设置 Puppeteer JS,以解决 reCaptcha v2。但是,您也可以将此方法应用于所有其他类型的验证码。
npm i puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
2. 设置扩展程序
下载扩展程序压缩包 ↗,并将其解压到项目根目录下的 ./CapSolver.Browser.Extension
文件夹中。
该扩展程序有许多设置,包括自动解决指定类型的验证码、支持代理和其他设置。这些设置在 ./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. 为使用 Capsolver 扩展程序解决 reCaptcha 设置 Puppeteer
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 ↗,并将验证码发送到 Capsolver。
使用 page.goto()
去到页面 https://www.google.com/recaptcha/api2/demo ↗。接下来,您需要手动或自动发送验证码进行解决。
在我们的示例中,我们将手动发送验证码,为此,我们等待具有 CSS 选择器 #recaptcha-anchor-label
的扩展程序按钮可用,然后单击该按钮。单击按钮后,验证码将进入服务以进行解决。
await page.goto('https://www.google.com/recaptcha/api2/demo')
// 等待具有 CSS 选择器 ".captcha-solver" 的元素可用
await page.waitForSelector('#recaptcha-anchor-label')
// 单击具有指定选择器的元素
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()
// 打开页面
await page.goto('https://www.google.com/recaptcha/api2/demo')
// 等待具有 CSS 选择器 "#recaptcha-anchor-label" 的元素可用
await page.waitForSelector('#recaptcha-anchor-label')
// 单击具有指定选择器的元素
await page.click('#recaptcha-anchor-label')
})();
这样就完成了使用 Puppeteer 和 Capsolver 扩展程序解决 reCaptcha v2 的过程。如果您需要解决其他验证码,只需为其他验证码复制该教程即可。
Capsolver团队 💜
加入我们
中文代理QQ群:497493756