Headless Chrome Node API 和 Puppeteer 安装
Headless Chrome Node API and Puppeteer installation
在整个安装过程中 chrome headless on a clean ubuntu 18.04 我遇到了很多问题。 github 上的设置指南对于干净的 ubuntu 18.04
是不够的
以下是设置 headless chrome phantomjs 替代品的一些错误和答案/解决方案。
错误 1
(node:23835) UnhandledPromiseRejectionWarning: Error: Chromium revision is not downloaded. Run "npm install" or "yarn install"
at Launcher.launch owlcommand.com /puppeteer/node_modules/puppeteer/lib/Launcher.js:112:15)
at <anonymous>
(node:23835) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:23835) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
错误 2
(node:25272) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
owlcommand.com /puppeteer/node_modules/puppeteer/.local-chromium/linux-594312/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory
基于https://github.com/GoogleChrome/puppeteer
你只需要 运行 在 Ubuntu 18.04
中执行以下命令
npm i puppeteer
不幸的是,这还不够。
您将需要以下依赖项
sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
之后如果你运行按照他们的例子,你会收到一个错误
(node:28469) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[1025/150325.817887:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
解决这个问题的方法是
const browser = await puppeteer.launch({args: ['--no-sandbox']});
添加 --no-sandbox
届时它将相应地工作。完整的工作源代码如下
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto('http://owlcommand.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
解决puppeteer@1.9.0~install: cannot 运行 in wd %s %s (wd=%s)
npm install --unsafe-perm
截图大小
默认值真的很小,如果你正在测试的页面是响应式的,你可以用不同的视口设置来测试它。您可以通过 setViewport 方法更改其尺寸。
await page.setViewport({
width: 1600,
height: 1000
});
最新 Puppeteer 更新(2020 年 8 月)
sudo apt-get install libgbm1(必需)
11 月 18 日更新:您不再需要 --no-sandbox 标志,您应该在发送给 .launch()[ 的对象中使用 headless:false 属性 =12=]
const browser = await puppeteer.launch({
headless: false,
slowMo: 80,
args: ['--window-size=1920,1080']
});
还要确保安装了所有必需的 debian 依赖项:
sudo apt-get install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
只有当我尝试 运行 Docker 上的节点应用程序时才会遇到这种问题,因此根据给出的答案,我终于可以使用 Docker文件:
FROM node:12
WORKDIR /app
COPY package.json /app/
RUN apt-get update \
&& apt-get install -y \
gconf-service \
libasound2 \
libatk1.0-0 \
libatk-bridge2.0-0 \
libc6 \
libcairo2 \
libcups2 \
libdbus-1-3 \
libexpat1 \
libfontconfig1 \
libgcc1 \
libgconf-2-4 \
libgdk-pixbuf2.0-0 \
libglib2.0-0 \
libgtk-3-0 \
libnspr4 \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libstdc++6 \
libx11-6 \
libx11-xcb1 \
libxcb1 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxi6 \
libxrandr2 \
libxrender1 \
libxss1 \
libxtst6 \
ca-certificates \
fonts-liberation \
libappindicator1 \
libnss3 \
lsb-release \
xdg-utils \
wget \
&& npm i puppeteer
COPY . /app
CMD [ "node", "app.js" ]
我也按照提到的方式更改了启动代码,这样效果很好:
const browser = await puppeteer.launch({args: ['--no-sandbox']});
我在 Windows 上遇到了同样的问题。我把asar设为false然后成功了
"build": { "asar":false, }
同时安装 libgbm1
"puppeteer": "^3.1.0"
完整命令是
apt-get update && apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
我正在使用 buildkite/puppeteer
的分支,发现使用新的 Chrome (85) 我必须安装 libxss1
而不是 libgbm1
(如其他建议评论)
所以现在看起来像这样 (link):
FROM node:12.18.3-buster-slim@sha256:dd6aa3ed10af4374b88f8a6624aeee7522772bb08e8dd5e917ff729d1d3c3a4f
RUN apt-get update \
&& apt-get install -y git \
&& apt-get update \
&& apt-get install -y libxss1 wget gnupg ca-certificates \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
# We install Chrome to get all the OS level dependencies, but Chrome itself
# is not actually used as it's packaged in the node puppeteer library.
# Alternatively, we could could include the entire dep list ourselves
# (https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix)
# but that seems too easy to get out of date.
&& apt-get install -y google-chrome-stable \
&& rm -rf /var/lib/apt/lists/* \
&& wget --quiet https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -O /usr/sbin/wait-for-it.sh \
&& chmod +x /usr/sbin/wait-for-it.sh
在整个安装过程中 chrome headless on a clean ubuntu 18.04 我遇到了很多问题。 github 上的设置指南对于干净的 ubuntu 18.04
是不够的以下是设置 headless chrome phantomjs 替代品的一些错误和答案/解决方案。
错误 1
(node:23835) UnhandledPromiseRejectionWarning: Error: Chromium revision is not downloaded. Run "npm install" or "yarn install"
at Launcher.launch owlcommand.com /puppeteer/node_modules/puppeteer/lib/Launcher.js:112:15)
at <anonymous>
(node:23835) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:23835) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
错误 2
(node:25272) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
owlcommand.com /puppeteer/node_modules/puppeteer/.local-chromium/linux-594312/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory
基于https://github.com/GoogleChrome/puppeteer
你只需要 运行 在 Ubuntu 18.04
中执行以下命令npm i puppeteer
不幸的是,这还不够。
您将需要以下依赖项
sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
之后如果你运行按照他们的例子,你会收到一个错误
(node:28469) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[1025/150325.817887:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
解决这个问题的方法是
const browser = await puppeteer.launch({args: ['--no-sandbox']});
添加 --no-sandbox
届时它将相应地工作。完整的工作源代码如下
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto('http://owlcommand.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
解决puppeteer@1.9.0~install: cannot 运行 in wd %s %s (wd=%s)
npm install --unsafe-perm
截图大小
默认值真的很小,如果你正在测试的页面是响应式的,你可以用不同的视口设置来测试它。您可以通过 setViewport 方法更改其尺寸。
await page.setViewport({
width: 1600,
height: 1000
});
最新 Puppeteer 更新(2020 年 8 月)
sudo apt-get install libgbm1(必需)
11 月 18 日更新:您不再需要 --no-sandbox 标志,您应该在发送给 .launch()[ 的对象中使用 headless:false 属性 =12=]
const browser = await puppeteer.launch({
headless: false,
slowMo: 80,
args: ['--window-size=1920,1080']
});
还要确保安装了所有必需的 debian 依赖项:
sudo apt-get install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
只有当我尝试 运行 Docker 上的节点应用程序时才会遇到这种问题,因此根据给出的答案,我终于可以使用 Docker文件:
FROM node:12
WORKDIR /app
COPY package.json /app/
RUN apt-get update \
&& apt-get install -y \
gconf-service \
libasound2 \
libatk1.0-0 \
libatk-bridge2.0-0 \
libc6 \
libcairo2 \
libcups2 \
libdbus-1-3 \
libexpat1 \
libfontconfig1 \
libgcc1 \
libgconf-2-4 \
libgdk-pixbuf2.0-0 \
libglib2.0-0 \
libgtk-3-0 \
libnspr4 \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libstdc++6 \
libx11-6 \
libx11-xcb1 \
libxcb1 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxi6 \
libxrandr2 \
libxrender1 \
libxss1 \
libxtst6 \
ca-certificates \
fonts-liberation \
libappindicator1 \
libnss3 \
lsb-release \
xdg-utils \
wget \
&& npm i puppeteer
COPY . /app
CMD [ "node", "app.js" ]
我也按照提到的方式更改了启动代码,这样效果很好:
const browser = await puppeteer.launch({args: ['--no-sandbox']});
我在 Windows 上遇到了同样的问题。我把asar设为false然后成功了
"build": { "asar":false, }
同时安装 libgbm1
"puppeteer": "^3.1.0"
完整命令是
apt-get update && apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
我正在使用 buildkite/puppeteer
的分支,发现使用新的 Chrome (85) 我必须安装 libxss1
而不是 libgbm1
(如其他建议评论)
所以现在看起来像这样 (link):
FROM node:12.18.3-buster-slim@sha256:dd6aa3ed10af4374b88f8a6624aeee7522772bb08e8dd5e917ff729d1d3c3a4f
RUN apt-get update \
&& apt-get install -y git \
&& apt-get update \
&& apt-get install -y libxss1 wget gnupg ca-certificates \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
# We install Chrome to get all the OS level dependencies, but Chrome itself
# is not actually used as it's packaged in the node puppeteer library.
# Alternatively, we could could include the entire dep list ourselves
# (https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix)
# but that seems too easy to get out of date.
&& apt-get install -y google-chrome-stable \
&& rm -rf /var/lib/apt/lists/* \
&& wget --quiet https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -O /usr/sbin/wait-for-it.sh \
&& chmod +x /usr/sbin/wait-for-it.sh