手动下载 Chrome,解决 puppeteer 无法使用问题

栏目: Node.js · 发布时间: 5年前

内容简介:因为网络原因,国内安装安装在

因为网络原因,国内安装 puppeteer 的时候会报网络超时。这里使用 puppeteer-core 之后使用手动下载的 Chrome 进行操作。思路很简单,安装一个不带浏览器的 puppeteer ,再使用的时候将浏览器地址指向一个可执行的 Chrome 浏览器文件。

安装

安装 puppeteer-core

yarn add puppeteer-core

找到 puppeteer 中对应的浏览器并下载

node_modules/puppeteer-core/lib/BrowserFetcher.js 中找到各平台 Chrome 下载地址。其中 %s 替换为 DEFAULT_DOWNLOAD_HOST 的值, %d 替换为版本号。

手动下载 Chrome,解决 puppeteer 无法使用问题

node_modules/puppeteer-core/packages.json 中找到版本号

手动下载 Chrome,解决 puppeteer 无法使用问题

替换后得到下载地址

https://storage.googleapis.com/chromium-browser-snapshots/Mac/579032/chrome-mac.zip

下载后解压,放在项目目录中,这里我放在 chrome 下。

使用

这样就可以使用了。

使用代码

const puppeteer = require('puppeteer-core');
const path = require('path');

(async () => {
    const browser = await puppeteer.launch({
        // 这里注意路径指向可执行的浏览器。
        // 各平台路径可以在 node_modules/puppeteer-core/lib/BrowserFetcher.js 中找到
        // Mac 为 '下载文件解压路径/Chromium.app/Contents/MacOS/Chromium'
        // Linux 为 '下载文件解压路径/chrome'
        // Windows 为 '下载文件解压路径/chrome.exe'
        executablePath: path.resolve('./chrome/Chromium.app/Contents/MacOS/Chromium')
    });
    const page = await browser.newPage();
    await page.setViewport({
        width: 375,
        height: 667,
        deviceScaleFactor: 1,
        isMobile: true
    })
    await page.goto('https://marxjiao.com/');
    await page.screenshot({path: 'marx-blog.png'});
    await browser.close();
})();

执行文件

node index.js

执行后可看到,图片已经截图出来了

手动下载 Chrome,解决 puppeteer 无法使用问题

代码地址: https://github.com/MarxJiao/puppeteer-test


以上所述就是小编给大家介绍的《手动下载 Chrome,解决 puppeteer 无法使用问题》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First Mobile Web

Head First Mobile Web

Lyza Danger Gardner、Jason Grigsby / O'Reilly Media / 2011-12 / $ 50.84

Despite the huge number of mobile devices and apps in use today, your business still needs a website. You just need it to be mobile. Head First Mobile Web walks you through the process of making a con......一起来看看 《Head First Mobile Web》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具