安装
🌐 Installation
要在项目中使用 Puppeteer,请运行:
🌐 To use Puppeteer in your project, run:
- npm
- Yarn
- pnpm
- Bun
npm i puppeteer
yarn add puppeteer
pnpm add puppeteer
bun add puppeteer
当你安装 Puppeteer 时,它会自动下载最近版本的测试用 Chrome(大约 170MB macOS,大约 282MB Linux,大约 280MB Windows)以及一个 chrome-headless-shell 二进制文件(从 Puppeteer v21.6.0 开始),该二进制文件已保证可与 Puppeteer 配合使用。默认情况下,浏览器会下载到 $HOME/.cache/puppeteer 文件夹(从 Puppeteer v19.0.0 开始)。有关配置选项和控制下载行为的环境变量,请参见配置。
🌐 When you install Puppeteer, it automatically downloads a recent version of
Chrome for Testing (~170MB macOS, ~282MB Linux, ~280MB Windows) and a chrome-headless-shell binary (starting with Puppeteer v21.6.0) that is guaranteed to
work
with Puppeteer. The browser is downloaded to the $HOME/.cache/puppeteer folder
by default (starting with Puppeteer v19.0.0). See configuration for configuration options and environmental variables to control the download behavior.
可以阻止自动下载
🌐 Automatic downloads can be blocked
许多现代包管理器(例如在新的 RFC 下的 npm、pnpm、Yarn Berry、Bun 和 Deno)默认阻止依赖安装脚本。如果你的包管理器被配置为阻止这些脚本,自动下载将被跳过,运行 Puppeteer 时会抛出错误:Could not find Chrome (ver. ...).
🌐 Many modern package managers (such as npm under the new RFC, pnpm, Yarn Berry, Bun, and Deno) block dependency install scripts by default. If your package manager is configured to block these scripts, the automatic download will be skipped, and running Puppeteer will throw an error: Could not find Chrome (ver. ...).
为了解决这个问题,你可以:
🌐 To resolve this, you can:
-
在安装包后手动安装浏览器,方法是运行:
- npm
- Yarn
- pnpm
- Bun
npx puppeteer browsers installyarn dlx puppeteer browsers installpnpm dlx puppeteer browsers installbun x puppeteer browsers install -
重新选择加入 以运行 Puppeteer 的 postinstall 脚本(例如,对于 npm,在你的
package.json中通过向"allowScripts"添加"puppeteer"):{"allowScripts": {"puppeteer": true}}
对于 v1.7.0 以来的每个版本,我们都会发布两个包:
🌐 For every release since v1.7.0 we publish two packages:
puppeteer 是一个用于浏览器自动化的 产品。安装后,它会下载一个 Chrome 版本,然后使用 puppeteer-core 来驱动它。作为一个面向终端用户的产品,puppeteer 使用合理的默认设置可以自定义 来自动化多个工作流程。
puppeteer-core 是一个用于驱动任何支持 DevTools 协议的 库。作为一个库,puppeteer-core 完全通过其编程接口进行操作,这意味着不假定任何默认值,并且安装时 puppeteer-core 不会下载 Chrome。
如果你正在连接到远程浏览器或自己管理浏览器,你应该使用 puppeteer-core。如果你自己管理浏览器,你需要调用 puppeteer.launch 并提供一个明确的 executablePath(或者如果它安装在标准位置,则使用 channel)。
🌐 You should use puppeteer-core if you are
connecting to a remote browser
or managing browsers yourself.
If you are managing browsers yourself, you will need to call
puppeteer.launch with
an explicit
executablePath
(or channel if it's
installed in a standard location).
使用 puppeteer-core 时,记得更改导入:
🌐 When using puppeteer-core, remember to change the import:
import puppeteer from 'puppeteer-core';