
一文讲透 AI Agent 与 AI Workflow 的区别和深度解析:从自动化到智能化的演进
本教程将为你详细介绍解决验证码的常见策略,帮助你绕过常见的验证码类型,包括手动输入、使用第三方验证码识别服务、模拟自动化登录过程、保存登录状态复用等。验证码的主要作用是防止机器人的自动化访问,而通过模拟人类行为,我们可以绕过这些限制。但破解验证码并不简单,需要根据验证码的类型采取不同的策略。
但一般来说,常见的验证码分类有:
图片验证码:如数字、字母的图形验证码,常见的有 reCAPTCHA
。
滑动验证码:需要模拟滑动的操作,如 Geetest
。
点击验证码:需要用户点击特定的图像区域,如交通标志、汽车等。
行为验证码:通过监测用户行为(如鼠标轨迹)来判定是否为人类。
特殊复杂验证:如语音、邮箱、手机短信、二维码扫描等
常见的破解策略有:
2Captcha
、AntiCaptcha
等,通过自动化服务来识别验证码。Playwright
的鼠标和键盘 API
来模拟用户行为。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
或其他图形验证码。API
密钥(如 2Captcha
)。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
,跳过验证码。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.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()
方法,我们将在后续的专题文章中进一步展开讨论。如果你觉得本系列教程对你有帮助,还请 点个赞,关个注,下次更新不迷路!