手動(dòng)下載Chrome并解決puppeteer無(wú)法使用問題
因?yàn)榫W(wǎng)絡(luò)原因,國(guó)內(nèi)安裝 puppeteer 的時(shí)候會(huì)報(bào)網(wǎng)絡(luò)超時(shí)。這里使用 puppeteer-core 之后使用手動(dòng)下載的 Chrome 進(jìn)行操作。思路很簡(jiǎn)單,安裝一個(gè)不帶瀏覽器的 puppeteer ,再使用的時(shí)候?qū)g覽器地址指向一個(gè)可執(zhí)行的 Chrome 瀏覽器文件。
安裝
安裝 puppeteer-core 。
yarn add puppeteer-core
找到 puppeteer 中對(duì)應(yīng)的瀏覽器并下載
在 node_modules/puppeteer-core/lib/BrowserFetcher.js 中找到各平臺(tái) Chrome 下載地址。其中 %s 替換為 DEFAULT_DOWNLOAD_HOST 的值, %d 替換為版本號(hào)。

在 node_modules/puppeteer-core/packages.json 中找到版本號(hào)

替換后得到下載地址
https://storage.googleapis.com/chromium-browser-snapshots/Mac/579032/chrome-mac.zip
下載后解壓,放在項(xiàng)目目錄中,這里我放在 chrome 下。
使用
這樣就可以使用了。
使用代碼
const puppeteer = require('puppeteer-core');
const path = require('path');
(async () => {
const browser = await puppeteer.launch({
// 這里注意路徑指向可執(zhí)行的瀏覽器。
// 各平臺(tái)路徑可以在 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();
})();
執(zhí)行文件
node index.js
執(zhí)行后可看到,圖片已經(jīng)截圖出來(lái)了

代碼地址:https://github.com/MarxJiao/puppeteer-test
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Puppeteer 爬取動(dòng)態(tài)生成的網(wǎng)頁(yè)實(shí)戰(zhàn)
- Puppeteer環(huán)境搭建的詳細(xì)步驟
- Puppet的一些技巧
- 詳解Puppeteer 入門教程
- node基于puppeteer模擬登錄抓取頁(yè)面的實(shí)現(xiàn)
- node puppeteer(headless chrome)實(shí)現(xiàn)網(wǎng)站登錄
- 詳解Node使用Puppeteer完成一次復(fù)雜的爬蟲
- 使用puppeteer破解極驗(yàn)的滑動(dòng)驗(yàn)證碼
- Node Puppeteer圖像識(shí)別實(shí)現(xiàn)百度指數(shù)爬蟲的示例
- 如何使用puppet替換文件中的string
相關(guān)文章
詳解Node.js使用token進(jìn)行認(rèn)證的簡(jiǎn)單示例
Node.js操作Firebird數(shù)據(jù)庫(kù)教程
express.js如何做mysql注入與node-mysql中防止SQL注入方法解析
在koa中簡(jiǎn)單使用Websocket連接的方法示例

