
一文講透 AI Agent 與 AI Workflow 的區別和深度解析:從自動化到智能化的演進
滑動驗證碼:需要模擬滑動的操作,如 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()
方法,我們將在后續的專題文章中進一步展開討論。如果你覺得本系列教程對你有幫助,還請 點個贊,關個注,下次更新不迷路!
原文轉載自:https://mp.weixin.qq.com/s/g-AdKRA-Sn9nqbI8jUiohA