Puppeteer 至少須要 Node v6.4.0,如要使用 async / await,只有 Node v7.6.0 或更高版本才支持。 node下載地址: https://nodejs.org/zh-cn/node
yarn add puppeteer 或者 npm i puppeteer
在安裝的過程當中遇到以下錯誤git
weifandeMacBook-Pro:example weifan$ npm i puppeteer --save > puppeteer@1.6.0 install /Users/weifan/Desktop/example/node_modules/puppeteer > node install.js ERROR: Failed to download Chromium r571375! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download. { Error: connect ETIMEDOUT 172.217.25.16:443 at Object._errnoException (util.js:999:13) at _exceptionWithHostPort (util.js:1020:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1207:14) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect', address: '172.217.25.16', port: 443 } npm WARN example@1.0.0 No description npm WARN example@1.0.0 No repository field. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! puppeteer@1.6.0 install: `node install.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the puppeteer@1.6.0 install script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /Users/weifan/.npm/_logs/2018-07-16T09_49_23_441Z-debug.log
報錯的緣由是:由於在執行安裝的過程當中須要執行install.js,這裏會下載Chromium,咱們這裏先跳過進行跳過,github
看來須要設置PUPPETEER_SKIP_CHROMIUM_DOWNLOAD,這個環境變量了,設置方法有多種,這裏以下:web
env PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true" npm i --save puppeteer
你會看到安裝成功chrome
下載地址:https://download-chromium.appspot.com/ npm
把下載剛剛下載的文件解壓到項目的chromium文件夾下,在chromium文件夾下你會看到chrome-mac文件,你能夠點擊愛看下問價內容。json
const puppeteer = require('puppeteer'); async function getPic() { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://google.com'); await page.screenshot({path: 'google.png'}); await browser.close(); } getPic();
運行代碼:node index.js,出現了以下錯誤api
(node:38213) UnhandledPromiseRejectionWarning: Error: Chromium revision is not downloaded. Run "npm install" or "yarn install" at assert (/Users/weifan/Desktop/example/node_modules/puppeteer/lib/helper.js:282:11) at Function.launch (/Users/weifan/Desktop/example/node_modules/puppeteer/lib/Launcher.js:106:7) at <anonymous>
顯示chromium 未下載錯誤,由於chromium默認的下載路徑是在node_modules/puppeteer/.local-chromium/目錄,這時候咱們的chromium是在項目根目錄,因此須要配置指定路徑,修改index.js文件:app
const puppeteer = require('puppeteer'); async function getPic() { const browser = await puppeteer.launch({ executablePath: '../chromium/chrome-mac/Chromium.app', headless: false }); const page = await browser.newPage(); await page.goto('https://google.com'); await page.screenshot({path: 'google.png'}); await browser.close(); } getPic();
再次運行index.js,又報以下錯誤:less
(node:38246) UnhandledPromiseRejectionWarning: Error: spawn EACCES
在puppeteer的Git issues找到以下解決方法,https://github.com/GoogleChrome/puppeteer/issues/1649,把executablePath改成以下:
executablePath: '../chromium/chrome-mac/Chromium.app/Contents/MacOS/Chromium',
再次node index.js 運行文件,能夠跑通了。
參考以下:
一、https://www.jianshu.com/p/a89d8d6c007b
二、https://blog.fundebug.com/2017/11/01/guide-to-automating-scraping-the-web-with-js/
三、https://github.com/GoogleChrome/puppeteer/issues/1649