安裝錯誤
安裝puppeteer時因為需要下載chrom的緣故總是安裝失敗提示:
ERROR: Failed to download Chromium r515411! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOA
D" env variable to skip download.
{ Error: read ECONNRESET
at _errnoException (util.js:1024:11)
at TLSWrap.onread (net.js:615:25) code: 'ECONNRESET', errno: 'ECONNRESET', s
yscall: 'read' }
npm WARN day2@1.0.0 No repository field.
複製程式碼
提示需要跳過安裝Chromiumset PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=1
解決辦法
執行npm install puppeteer --ignore-scripts
跳過安裝chromuin步驟
然後在指令碼中通過配置項 executablePath
,指定 Chromium 所在的位置。示例:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
executablePath: '../../chromium/Chromium.app/Contents/MacOS/Chromium'
});
const page = await browser.newPage();
await page.goto('https://y.qq.com');
await page.screenshot({path: 'yqq.png'});
browser.close();
})();
複製程式碼
手動下載Chromuim
牆裡下載不到Chromuim怎麼辦。看倉庫原始碼可以發現下載地址是這樣的
const downloadURLs = {
linux: 'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/%d/chrome-linux.zip',
mac: 'https://storage.googleapis.com/chromium-browser-snapshots/Mac/%d/chrome-mac.zip',
win32: 'https://storage.googleapis.com/chromium-browser-snapshots/Win/%d/chrome-win32.zip',
win64: 'https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip'
}
複製程式碼
選擇對應的平臺,將 %d
替換成具體的編號,編號可以從 puppeteer/package.json
中的 puppeteer.chromium_revision
檢視
然後執行我們的程式碼就一切正常了。
哎! ,國外大公司新出的框架,庫,工具我們我們大天朝內使用太難了,總是各種採坑,今天在家搞了半天才把puppeteer
執行正常