验证码的主要作用是防止机器人的自动化访问,而通过模拟人类行为,我们可以绕过这些限制。但破解验证码并不简单,需要根据验证码的类型采取不同的策略。
验证码类型及破解策略
验证码类型
但一般来说,常见的验证码分类有:
1. 图片验证码:如数字、字母的图形验证码,常见的有 reCAPTCHA
。
2. 滑动验证码:需要模拟滑动的操作,如 Geetest
。
3. 点击验证码:需要用户点击特定的图像区域,如交通标志、汽车等。
4. 行为验证码:通过监测用户行为(如鼠标轨迹)来判定是否为人类。
5. 特殊复杂验证:如语音、邮箱、手机短信、二维码扫描等
破解策略
常见的破解策略有:
1. 手动输入:适合少量数据抓取,但不适合全自动化。
2. 第三方验证码识别服务:如 2Captcha
、AntiCaptcha
等,通过自动化服务来识别验证码。
3. 自动化模拟用户行为:使用 Playwright
的鼠标和键盘 API
来模拟用户行为。
4. 复用登录信息:通过保存并复用浏览器登录状态(如cookies
)来跳过验证码。
手动输入验证码 – 最简单的解决方案
对于一些频繁遇到验证码的页面,手动输入验证码虽然简便,但无法完全自动化。适用于少量数据抓取场景。
适用场景
• 一次性爬取,或者抓取少量数据时。
• 验证码简单,且不频繁出现。
解决方法
当遇到验证码时,手动输入后继续抓取。此方法简单直接,但显然无法适应大规模爬取。
在 Playwright
中,可以使用 page.pause()
方法暂停脚本执行,等待手动输入验证码。
示例
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://www.example.com/login');
// 输入用户名和密码
await page.fill('#username', 'your-username');
await page.fill('#password', 'your-password');
// 暂停脚本,等待手动输入验证码
console.log('请在浏览器中手动输入验证码,然后按回车继续...');
await page.pause();
// 点击登录按钮
await page.click('#login-button');
await browser.close();
})();
使用第三方验证码识别服务
使用第三方验证码识别服务(如 2Captcha
或 Anti-Captcha
)来自动识别验证码。
适用场景:
• 适合较为复杂的验证码,如 reCAPTCHA
或其他图形验证码。
• 适合需要自动化且验证码复杂的场景。
实现方法
1. 注册并获取 API
密钥(如 2Captcha
)。
2. 截取验证码图片。
3. 上传图片进行识别,并返回验证码文本。
4. 输入验证码并提交。
示例:使用 2Captcha
识别验证码
const { chromium } = require('playwright');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
(async () => {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
// 打开包含验证码的页面
await page.goto('https://example.com/captcha');
// 截取验证码图片
const captchaImg = await page.locator('.captcha-image').screenshot();
fs.writeFileSync(path.join(__dirname, 'captcha.png'), captchaImg);
// 使用 2Captcha API 进行验证码识别
const captchaResult = await solveCaptchaWith2Captcha('YOUR_API_KEY', 'captcha.png');
// 输入验证码
await page.fill('#captcha-input', captchaResult);
await page.click('#submit-button');
console.log('验证码已解决并提交');
await browser.close();
})();
async function solveCaptchaWith2Captcha(apiKey, imagePath) {
const image = fs.readFileSync(path.join(__dirname, imagePath));
const response = await axios.post(`http://2captcha.com/in.php?key=${apiKey}&method=userrecaptcha&body=${image}`, image);
const captchaId = response.data.request;
// 查询识别结果
const result = await axios.get(`http://2captcha.com/res.php?key=${apiKey}&action=get&id=${captchaId}`);
return result.data.request;
}
自动化模拟用户行为 – 解决滑动、点击验证码
有些验证码(如 Geetest
、reCAPTCHA
)通过模拟用户行为来验证是否为人类。这些验证码类型要求用户执行某些操作(如滑动条、点击指定区域等),我们可以使用 Playwright
来模拟这些用户行为,绕过验证码。
适用场景
• 适合滑动、点击等行为验证码。
• 避免使用第三方验证码识别服务,提高自动化效率。
解决方法:
• 使用 Playwright
的 mouse API
模拟滑动或点击操作。
• 通过 mouse.move()
、mouse.down()
、mouse.up()
等函数模拟用户的滑动或点击行为。
示例:模拟滑动验证码
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
// 打开包含滑动验证码的页面
await page.goto('https://www.geetest.com/demo');
// 等待验证码加载
await page.waitForSelector('.geetest_canvas_bg');
// 获取滑动验证码元素
const slideElement = await page.locator('.geetest_slider_button');
// 模拟滑动动作
const box = await slideElement.boundingBox();
const startX = box.x + box.width / 2;
const startY = box.y + box.height / 2;
await page.mouse.move(startX, startY);
await page.mouse.down();
await page.mouse.move(startX + 200, startY, { steps: 20 });
await page.mouse.up();
console.log('滑动验证码已解决');
await browser.close();
})();
复用登录信息,绕过验证码
在一些网站中,验证码通常只出现在登录或注册时。为了避免每次都遇到验证码,我们可以通过保存并复用浏览器的登录状态来绕过验证码。
适用场景
• 需要频繁登录的网站。
• 登录过程复杂,且需要验证码、短信验证码、二维码扫描等。
实现方法
使用 Playwright
的 storageState
功能保存登录状态,并在后续脚本中复用。
- 登录并保存登录状态
(Cookies)
。 - 在后续请求中加载
Cookies
,跳过验证码。
示例:保存和复用 Cookies
const { chromium } = require('playwright');
// 保存登录状态
(async () => {
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://www.example.com/login');
await page.fill('#username', 'your-username');
await page.fill('#password', 'your-password');
await page.click('#login-button');
// 保存登录状态
await context.storageState({ path: 'login_state.json' });
await browser.close();
})();
// 复用登录状态
(async () => {
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext({ storageState: 'login_state.json' });
const page = await context.newPage();
await page.goto('https://www.example.com');
console.log(await page.content());
await browser.close();
})();
Playwright Connect
– 复用浏览器会话
适用场景
• 需要手动登录的网站。
• 登录过程复杂,且需要验证码、短信验证码、二维码扫描等。
实现方法
playwright.chromium.connect_over_cdp()
连接到已打开的 Chromium
浏览器实例,复用登录状态,可以避免每次启动浏览器时都要重新登录或解决验证码。
示例代码
const { chromium } = require('playwright');
(async () => {
// 连接到已打开的 Chromium 浏览器实例
const browser = await chromium.connectOverCDP('http://localhost:9222');
const context = browser.contexts()[0];
const page = context.pages()[0];
await page.goto('https://www.example.com');
console.log(await page.content());
await browser.close();
})();
总结
在爬虫开发中,验证码是一个常见的反爬虫手段。面对验证码,我们可以通过以下策略应对:
• 手动输入验证码:适合小规模抓取。
• 使用第三方验证码识别服务:适合复杂验证码,提供自动化识别方案。
• 模拟用户行为:通过 Playwright
模拟用户行为(滑动、点击等),绕过验证码。
• 复用登录状态:保存并复用浏览器的登录状态,减少验证码出现的频率。
对于更深入的技术,如何复用登录信息以及使用 connect_over_cdp()
方法,我们将在后续的专题文章中进一步展开讨论。
暂无评论内容