Ubuntu 安装Puppeteer

放几个阿里云的优惠链接 代金券 / 高性能服务器2折起 / 高性能服务器5折

添加 puppeteer

npm i --save puppeteer
  • 安装时会因为国内外网屏蔽导致下载失败出现
ERROR: Failed to download Chromium r515411! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download. 

可以使用国内镜像解决,参考

npm config set puppeteer_download_host=https://npm.taobao.org/mirrors
npm i puppeteer

  • 运行出现 Error: Failed to launch chrome
(node:11679) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Failed to launch chrome!
/home/ss/test_work/gp/node_modules/puppeteer/.local-chromium/linux-579032/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory


TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

(node:11679) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

官方现在已经给出解决方法,参考。

安装缺少的依赖,下面是列出的依赖,可以通过 apt 安装

gconf-service
libasound2
libatk1.0-0
libatk-bridge2.0-0
libc6
libcairo2
libcups2
libdbus-1-3
libexpat1
libfontconfig1
libgcc1
libgconf-2-4
libgdk-pixbuf2.0-0
libglib2.0-0
libgtk-3-0
libnspr4
libpango-1.0-0
libpangocairo-1.0-0
libstdc++6
libx11-6
libx11-xcb1
libxcb1
libxcomposite1
libxcursor1
libxdamage1
libxext6
libxfixes3
libxi6
libxrandr2
libxrender1
libxss1
libxtst6
ca-certificates
fonts-liberation
libappindicator1
libnss3
lsb-release
xdg-utils
wget
  • 设置args
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
  • 安装结束,运行示例可以将网页保存未图片快照
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

你可能感兴趣的:(开发笔记)