Puppeteer中使用Stealth.min.js库

本文介绍了如何在Node.js中安装并启用puppeteer-extra-plugin-stealth,以便在Puppeteer启动浏览器时自动应用Stealth功能,实现对网站行为的隐身访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里需要安装npm install puppeteer-extra puppeteer-extra-plugin-stealth,然后,在启动浏览器时,Puppeteer 会自动应用 Stealth.min.js 插件的功能。

const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');

async function removeBrowserFeatures() {
  puppeteer.use(StealthPlugin());

  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto('https://example.com');

  // 执行其他操作...

  await browser.close();
}

removeBrowserFeatures();

### 如何下载并使用 stealth.min.js 文件 #### 下载方法 可以通过多个途径获取 `stealth.min.js` 文件: 1. **通过 GitCode 获取** 可访问项目地址 [GitCode](https://gitcode.com/open-source-toolkit/827ab)[^1],找到对应的资源文件进行下载。 2. **通过 GitHub Actions 自动生成版本** 前往 [requireCool/stealth.min.js](https://github.com/requireCool/stealth.min.js)[^3] 的仓。该仓会定期自动生成最新版的 `stealth.min.js` 文件,可以直接从 Releases 页面下载最新的构建版本。 3. **手动生成 `stealth.min.js` 文件** 如果希望自行生成此文件,可参考 [puppeteer-extra](https://github.com/puppeteer-extra/puppeteer-extra-plugin-stealth)[^2] 仓中的 `evasions` 文件夹内容。将其中的所有反检测脚本提取出来,并打包成单个 `stealth.min.js` 文件供后续使用。 --- #### 使用方法 以下是基于 Selenium 和 Playwright 框架的具体实现方式: ##### 在 Selenium 中加载 `stealth.min.js` 以下是一个完整的 Python 实现示例,展示如何利用 `stealth.min.js` 隐藏浏览器指纹特征[^4]: ```python from selenium import webdriver # 浏览器配置选项 options = webdriver.ChromeOptions() options.add_argument('--headless') # 无头模式运行 options.add_argument('--disable-gpu') options.add_argument( 'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' '(KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36' ) # 初始化 WebDriver 并加载 JavaScript 文件 driver = webdriver.Chrome(options=options) with open('stealth.min.js', mode='r', encoding='utf-8') as f: js = f.read() # 将 JS 注入到页面上下文中 driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": js}) # 请求目标网站 driver.get('https://bot.sannysoft.com/') driver.save_screenshot('output.png') # 关闭驱动程序 driver.quit() ``` ##### 在 Pyppeteer 或 Playwright 中加载 `stealth.min.js` 对于 Playwright 用户,可以按照如下方式进行设置[^5]: ```python import asyncio from playwright.async_api import async_playwright async def main(): async with async_playwright() as p: browser = await p.chromium.launch(headless=False) context = await browser.new_context() # 加载 stealth.min.js 脚本 with open('stealth.min.js', 'r', encoding='utf-8') as file: await context.add_init_script(file.read()) page = await context.new_page() await page.goto('https://bot.sannysoft.com/') await page.screenshot(path="example.png") await browser.close() asyncio.run(main()) ``` --- #### 注意事项 1. 确保本地已正确放置 `stealth.min.js` 文件,并且路径指向准确。 2. 不同框架可能对注入脚本的方式有所差异,请根据实际需求调整代码逻辑。 3. 若需进一步增强隐身效果,建议结合其他反爬技术(如修改 User-Agent、禁用 GPU 渲染等)共同实施。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值