阅读 886

puppeteer 安装失败的解决办法

安装错误

安装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运行正常

评论