📅  最后修改于: 2023-12-03 15:18:43.592000             🧑  作者: Mango
Puppeteer is a Node.js library that provides a high-level API for controlling headless Chrome or Chromium browsers. It is widely used for web scraping, automation testing, and creating bots.
However, if you're using a Mac with an M1 chip, you might encounter some issues when trying to run Puppeteer. In this article, we'll discuss how to set up and use Puppeteer on a Mac with an M1 chip.
Before you start using Puppeteer on your Mac with an M1 chip, make sure you have the following prerequisites:
Puppeteer uses Chromium as its default browser. Unfortunately, the version of Chromium that Puppeteer installs using NPM is not compatible with Macs with M1 chips.
To install Chromium manually, you can use Homebrew. First, install Homebrew using the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then, install Chromium using the following command:
brew install --cask chromium
Once you've installed Chromium, you can install Puppeteer using NPM:
npm install puppeteer
However, the version of Puppeteer that NPM installs is not compatible with Macs with M1 chips. To install a compatible version, you can use the following command:
npm install puppeteer-core@^9.1.1
Now that you've installed Puppeteer and Chromium, you can use Puppeteer in your Javascript code as you would normally.
Here's an example script that navigates to Google and takes a screenshot:
const puppeteer = require('puppeteer-core');
(async () => {
const browser = await puppeteer.launch({
executablePath: '/Applications/Chromium.app/Contents/MacOS/Chromium',
});
const page = await browser.newPage();
await page.goto('https://www.google.com');
await page.screenshot({path: 'google.png'});
await browser.close();
})();
Make sure to provide the executablePath
option when launching Puppeteer, pointing it to the Chromium executable.
That's it! By following these steps, you should now be able to use Puppeteer on your Mac with an M1 chip. Keep in mind that this version of Puppeteer might not have all the features of the latest version, but it should work for most use cases.