參考:https://lzw.me/a/puppeteer-install-skip-download-chrome.html
puppeteer 安裝時跳過 chrome 安裝的方法
puppeteer 安裝時跳過下載 chrome
在項目目錄里安裝 puppeteer 時,都會下載 Chromium。這個慢不說,也會造成重復下載、耗時過長等問題。那么可以這么來做跳過 Chromium 的下載:
1
|
npm
install
puppeteer --ignore-scripts
|
然后在腳本中通過配置項 executablePath
,指定 Chromium 所在的位置(也可以指定為本地 Chrome 瀏覽器安裝的地址)。示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
const puppeteer = require(
'puppeteer'
);
puppeteer.launch({
headless:
false
,
// executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
executablePath:
'C:\\Program Files\\nodejs\\node_modules\\puppeteer\\.local-chromium\\win64-508693\\chrome-win32\\chrome.exe'
}).then(
async
browser => {
const page =
await
browser.newPage();
await
page.screenshot({path:
'lzwme.png'
});
browser.close();
}).
catch
(err => {
console.log(err);
});
|
提示:
當前淘寶倉庫已經有該鏡像,可以通過設置環境變量方式從該鏡像地址進行下載安裝。命令示例:
1
2
3
|
# window
set
PUPPETEER_DOWNLOAD_HOST=https:
//npm
.taobao.org
/mirrors
npm
install
puppeteer
|
手動下載 chrome
有的同學可能會問,在牆里下載不到開發版的 chrome 怎么辦。翻看倉庫源碼可以得知,下載地址是這樣的:
1
2
3
4
5
6
|
const downloadURLs = {
};
|
選擇對應的平台,將 %d
替換成具體的編號,然后使用迅雷這種 P2P 下載即可。這個編號可以從 puppeteer/package.json
中的 puppeteer.chromium_revision
字段獲得。
另外,你也可以從淘寶鏡像倉庫進行下載,地址如下:
https://npm.taobao.org/mirrors/chromium-browser-snapshots/
相關參考:
https://github.com/GoogleChrome/puppeteer/issues/244
https://github.com/GoogleChrome/puppeteer/issues/288
關於 puppeteer
在以前,當需要進行無界面的瀏覽器自動訪問網站執行動作時,大都會選擇 phantomjs 或 SlimerJS。
在 https://lzw.me/a/slimerjs-phantomjs-casperjs.html 這篇文章中我們對 phantomjs 進行了簡單的使用介紹。
puppeteer 則是 chrome 官方團隊出品的同類工具。當 puppeteer 出來后,phantomjs 即宣布不再繼續開發維護。puppeteer 的使用很簡單,功能更豐富,可以通過腳本實現各種自動化功能需求。
本文不對 puppeteer 具體使用作詳細介紹。關於 puppeteer 的使用示例可參考這幾篇文章:
https://github.com/laispace/puppeteer-explore
https://github.com/zhentaoo/puppeteer-deep
https://cloud.tencent.com/community/article/529168
https://github.com/GoogleChrome/puppeteer