在我的Linux服务器上,我运行我的nodejs
项目,该项目应通过puppeteer
npm模块抓取单页应用程序。
这里是我使用的代码示例:
const puppeteer = require('puppeteer');
(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://bvopen.abrickis.me/#/standings');
await page.waitForSelector('.category', { timeout: 1000 });
const body = await page.evaluate(() => {
return document.querySelector('body').innerHTML;
});
console.log(body);
await browser.close();
} catch (error) {
console.log(error);
}
})();
但是我遇到下一个错误:
0|www | Error: Failed to launch the browser process!
0|www | [5642:5642:0511/154701.856738:ERROR:browser_main_loop.cc(1485)] Unable to open X display.
0|www | [0511/154701.863486:ERROR:nacl_helper_linux.cc(308)] NaCl helper process running without a sandbox!
0|www | Most likely you need to configure your SUID sandbox correctly
0|www | TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md
我研究了很多如何安装X服务器的方法,并尝试了很多类似sudo apt-get install xorg openbox
的方法,但这没有帮助。
看来puppeteer希望以非无头模式启动浏览器,但是由于您没有安装xorg,因此它失败了。但是我要说的是,无论如何,这不是您想要的。因此,不需要安装xorg或任何东西。
也许尝试使用以下选项启动木偶浏览器:
await puppeteer.launch({
headless: true,
args: [
"--disable-gpu",
"--disable-dev-shm-usage",
"--no-sandbox",
"--disable-setuid-sandbox"
});
希望成功。