利用Playwright Stealth来避免自动化检测

Python

安装 Playwright Stealth 插件:

pip install playwright-stealth

打开 index.py 文件,将下面的导入内容添加到 Playwright 脚本中:

from playwright_stealth import stealth_async

或者,如果您使用的是同步 API:

from playwright_stealth import stealth_sync

要在 Playwright 中注册,请将 page 对象传递给导入的函数,如下所示:

await stealth_async(page)

或者,如果您使用的是同步 API:

stealth_async(page)

stealth_async() 功能将通过覆盖某些默认配置来扩展 page 功能,以避免僵尸检测。

JS

首先,安装 playwright-extrapuppeteer-extra-plugin-stealth

npm install playwright-extra puppeteer-extra-plugin-stealth

接下来,从 playwright-extra 导入 chromium 而不是 playwright ,从 puppeteer-extra-plugin-stealth 导入 StealthPlugin

import { chromium } from "playwright-extra"
import StealthPlugin from "puppeteer-extra-plugin-stealth"

然后,用以下方式注册隐身插件:

chromium.use(StealthPlugin())

把这一切放在一起,你就会得到:

import { chromium } from "playwright-extra"
import StealthPlugin from "puppeteer-extra-plugin-stealth"

(async () => {
// configure the Stealth plugin
chromium.use(StealthPlugin())

// set up the browser and launch it
const browser = await chromium.launch()
// open a new blank page
const page = await browser.newPage()

// navigate the page to the target page
await page.goto("https://arh.antoinevastel.com/bots/areyouheadless")

// extract the message contained on the page
const messageElement = page.locator('#res')
const message = await messageElement.textContent()

// print the resulting message
console.log(`The result is: "${message}"`)

// close the browser and release its resources
await browser.close()
})()

参考