如何使用puppeteer-core,puppeteer使用本地Chrome,puppeteer下載失敗問題


puppeteer由於眾所周知的原因會下載失敗,我們可以使用puppeteer-core配合本地的Chrome或者Chrome Canary來使用:
首先安裝puppeteer-core和Carlo(會用到Carlo的find_chrome模塊,可以在node_modules/carlo/lib/目錄下找到)
//安裝對應chrome@71對應的puppeteer
npm install puppeteer-core@chrome-71
//安裝Carlo
npm i carlo
//或 yarn add carlo
不同版本的瀏覽器可以使用對應版本的puppeteer-core,以減少不兼容問題。
具體的puppeteer對應的Chrome版本查看:https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md 中的Releases per Chromium Version部分
const puppeteer = require('puppeteer-core');
//find_chrome模塊來源於GoogleChromeLabs的Carlo,可以查看本機安裝Chrome目錄,詳細請查看底部博客,

const findChrome = require('./node_modules/carlo/lib/find_chrome');

(async () => {
  let findChromePath = await findChrome({});
  let executablePath = findChromePath.executablePath;
  console.log(executablePath)
  const browser = await puppeteer.launch({
    executablePath,
    headless: false
  });

  const page = await browser.newPage();
  await page.goto('http://www.woleigequ.net/');
  /* dosomeThing */

  await browser.close();
})();
以上代碼部分來自下面的博客:

Web技術試煉地

chaos-fe


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM