如何在Puppeteer中使用Capsolver插件解决reCaptcha v2

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。如果您需要解决其他类型的验证码,只需按照相应的教程进行复制即可。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值