Linux安装部署Puppeteer踩坑

linux直接npm install puppeteer -g报错

安装puppeteer

# 忽略Chromium安装
npm install puppeteer --ignore-scripts --save
# 下载一个Chromium放到指定位置
# linux: '',
# mac: '',
# win32: '',
# win64: '',

# 下载时将%d替换为具体编号,编号可以在node_modules/puppeteer/package.json中puppeteer.chromium_revision获得。拼接好链接后直接在浏览器就可以直接下载了(当然,你得科学上网才可以)。

# 下载后上传到服务器,存储位置为node_modules/puppeteer/.local-chromium/linux-%d,.local-chromium/linux-%d文件夹需要自己创建,同样将%d替换为具体编号。将下载的Chromium压缩包上传到该位置后解压。

# "puppeteer": "^2.0.0",
# 
复制代码

安装相关依赖

linux (Centos)上使用 puppeteer 还需要安装一些依赖

#依赖库
yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 nss.x86_64 -y
#字体
yum install ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc -y
复制代码

常见报错

权限问题

Error: spawn /home/card-spider/node_modules/puppeteer/.local-chromium/linux-706915/chrome-linux/chrome EACCES
    at Process.ChildProcess._handle.onexit (internal/child_process.js:232:19)
    at onErrorNT (internal/child_process.js:407:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:238:12)
    at onErrorNT (internal/child_process.js:407:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
npm ERR! code ELIFECYCLE
复制代码
  • 处理权限
sudo chmod -R 777 /node_modules/puppeteer/.local-chromium/linux-706915/chrome-linux/chrome
复制代码

沙箱问题

let launchConfig = {
  headless: false,
  timeout: 10 * 60 * 1000,
  defaultViewport: { width: 1366, height: 768 }
}
console.log('环境:' + env)
if (env === 'production') {
  launchConfig = {
    timeout: 10 * 60 * 1000,
    args: ['--no-sandbox', '--disable-setuid-sandbox']
  }
}
复制代码

超时问题

  • 延长 timeout

Ubuntu安装踩坑

  1. 依赖
# 依赖安装
sudo apt install -y gconf-service libasound2 libatk1.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

复制代码
  1. 参数 args: ['--no-sandbox', '--disable-setuid-sandbox']

参考文档

  • centos安装puppeteer爬坑
  • ubuntu puppeteer爬坑

作者:oncetry

链接:https://juejin.im/post/6844903987947257870

 

你可能感兴趣的:(软件研发,java,docker,linux,chrome,vue)