要把puppeteer正常安裝部署到CentOS7上並能正常顯示網頁,不像在Windows上那麼簡單,網上建議用現成的docker鏡像,我這裏總結下直接安裝方案,其實也超簡單。javascript
npm install cnpm -g
;若是服務器在牆外則能夠忽略此步安裝puppeteer.java
cnpm install
cnpm install puppeteer
ldd node_modules/puppeteer/.local-chromium/linux-706915/chrome-linux/chrome
yum install -y alsa-lib.x86_64 atk.x86_64 cups-libs.x86_64 GConf2.x86_64 gtk3.x86_64 ipa-gothic-fonts libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXrandr.x86_64 libXScrnSaver.x86_64 libXtst.x86_64 pango.x86_64 wqy-unibit-fonts.noarch wqy-zenhei-fonts.noarch xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-fonts-cyrillic xorg-x11-fonts-misc xorg-x11-fonts-Type1 xorg-x11-utils
至此,安裝完成,能夠驗證了。node
編寫index.js,內容以下:linux
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']}); const page = await browser.newPage(); await page.goto('https://www.segmentfault.com'); await page.screenshot({path: 'sf.png'}); await browser.close(); })();
注意:
1) 必須加上--no-sandbox
參數啓動chrome,不然報錯
2) 默認就是以無頭(無界面)模式啓動chrome,即自帶--headless
參數git
node index.js
執行上述腳本,會在當前目錄下產生思否首頁截圖,能夠打開看看,驗證是否正常顯示中文