这里专门开个帖子用来整理采集遇到的问题

PHP采集时报错Failed to launch the browser process puppeteer

PHPer 2022-04-24 69 0 0
Chrome headless doesn't launch on UNIX,谷歌无头浏览器不能在unix运行,要安装对应的依赖侯才能正常运行无头模式。

具体解决办法参看 https://wokan.chawen.org/post/1167

Chrome headless doesn't launch on Windows

Some chrome policies might enforce running Chrome/Chromium with certain extensions.

Puppeteer passes --disable-extensions flag by default and will fail to launch when such policies are active.

To work around this, try running without the flag:

const browser = await puppeteer.launch({

ignoreDefaultArgs: ['--disable-extensions'],

});

Context: issue 3681.

Chrome headless doesn't launch on UNIX

Make sure all the necessary dependencies are installed. You can run ldd chrome | grep not on a Linux machine to check which dependencies are missing. The common ones are provided below.

Debian (e.g. Ubuntu) Dependencies (Debian系统依赖)

ca-certificates
fonts-liberation
libappindicator3-1
libasound2
libatk-bridge2.0-0
libatk1.0-0
libc6
libcairo2
libcups2
libdbus-1-3
libexpat1
libfontconfig1
libgbm1
libgcc1
libglib2.0-0
libgtk-3-0
libnspr4
libnss3
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
lsb-release
wget
xdg-utils

CentOS Dependencies (Centos系统依赖)

alsa-lib.x86_64
atk.x86_64
cups-libs.x86_64
gtk3.x86_64
ipa-gothic-fonts
libXcomposite.x86_64
libXcursor.x86_64
libXdamage.x86_64
libXext.x86_64
libXi.x86_64
libXrandr.x86_64
libXScrnSaver.x86_64
libXtst.x86_64
pango.x86_64
xorg-x11-fonts-100dpi
xorg-x11-fonts-75dpi
xorg-x11-fonts-cyrillic
xorg-x11-fonts-misc
xorg-x11-fonts-Type1
xorg-x11-utils

After installing dependencies you need to update nss library using this command

yum update nss -y


Check out discussions

Chrome headless disables GPU compositing

Chrome/Chromium requires --use-gl=egl to enable GPU acceleration in headless mode.

const browser = await puppeteer.launch({
  headless: true,
  args: ['--use-gl=egl'],
});

Chrome is downloaded but fails to launch on Node.js 14

If you get an error that looks like this when trying to launch Chromium:

(node:15505) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!

spawn /Users/.../node_modules/puppeteer/.local-chromium/mac-756035/chrome-mac/Chromium.app/Contents/MacOS/Chromium ENOENT

This means that the browser was downloaded but failed to be extracted correctly. The most common cause is a bug in Node.js v14.0.0 which broke extract-zip, the module Puppeteer uses to extract browser downloads into the right place. The bug was fixed in Node.js v14.1.0, so please make sure you're running that version or higher. Alternatively, if you cannot upgrade, you could downgrade to Node.js v12, but we recommend upgrading when possible.

Setting Up Chrome Linux Sandbox

In order to protect the host environment from untrusted web content, Chrome uses multiple layers of sandboxing. For this to work properly, the host should be configured first. If there's no good sandbox for Chrome to use, it will crash with the error No usable sandbox!.

If you absolutely trust the content you open in Chrome, you can launch Chrome with the --no-sandbox argument:

const browser = await puppeteer.launch({

args: ['--no-sandbox', '--disable-setuid-sandbox'],

});

NOTE: Running without a sandbox is strongly discouraged. Consider configuring a sandbox instead.

There are 2 ways to configure a sandbox in Chromium.

[recommended] Enable user namespace cloning

User namespace cloning is only supported by modern kernels. Unprivileged user namespaces are generally fine to enable, but in some cases they open up more kernel attack surface for (unsandboxed) non-root processes to elevate to kernel privileges.

sudo sysctl -w kernel.unprivileged_userns_clone=1

[alternative] Setup setuid sandbox

The setuid sandbox comes as a standalone executable and is located next to the Chromium that Puppeteer downloads. It is fine to re-use the same sandbox executable for different Chromium versions, so the following could be done only once per host environment:

# cd to the downloaded instance

cd /node_modules/puppeteer/.local-chromium/linux-/chrome-linux/

sudo chown root:root chrome_sandbox

sudo chmod 4755 chrome_sandbox

# copy sandbox executable to a shared location

sudo cp -p chrome_sandbox /usr/local/sbin/chrome-devel-sandbox

# export CHROME_DEVEL_SANDBOX env variable

export CHROME_DEVEL_SANDBOX=/usr/local/sbin/chrome-devel-sandbox

You might want to export the CHROME_DEVEL_SANDBOX env variable by default. In this case, add the following to the ~/.bashrc or .zshenv: ...

立即注册
分享的网址网站均收集自搜索引擎以及互联网,非查问网运营,查问网并没有提供其服务,请勿利用其做侵权以及违规行为。

更新于:2022-06-30 11:48:18
    您需要登录后才可以评论。 立即注册
    相关内容

    Centos7利用yum安装apache+mysql8+php7 一篇搞定!

    PHP错误:SQLSTATE[HY000] [2054] The server requested authentica...

    PHP报错:Constants may only evaluate to scalar values

    Centos中查看nginx、apache、php、mysql配置文件路径

    php报错 Error: Variable 'sql_mode' can't be set to the value of 'M...

    推荐内容

    分享几个好用的bt搜索bt资源下载网址网站

    影视电影剧集动漫综艺bt资源在线播放网址网站推荐分享

    全网最新bt磁力搜索引擎bt资源bt网站网址分享

    怎样使用V2Ray代理和SSTap玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩...

    sstap游戏代理教程 从此玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩虹六...

    使用V2Ray的mKCP协议加速游戏

    v2rayN已停止工作