Puppeteer 错误:导航失败,因为浏览器已断开连接

Hum*_*der 10 google-app-engine node.js google-cloud-platform puppeteer

我在 Google App Engine 上使用 puppeteer 和 Node.JS

每当我在应用程序引擎上运行 puppeteer 时,我都会遇到一条错误消息

导航失败,因为浏览器已断开连接!

这在本地环境中工作正常,所以我猜测这是应用程序引擎的问题。

const browser = await puppeteer.launch({
    ignoreHTTPSErrors: true,
    headless: true,
    args: ["--disable-setuid-sandbox", "--no-sandbox"],
});
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序引擎的 app.yaml 文件

runtime: nodejs12

env: standard

handlers:
  - url: /.*
    secure: always
    script: auto
Run Code Online (Sandbox Code Playgroud)

- 编辑 -

当我添加--disable-dev-shm-usage参数时它有效,但它总是超时。这是我的代码。

const browser = await puppeteer.launch({
  ignoreHTTPSErrors: true,
  headless: true,
  args: [
    "--disable-gpu",
    "--disable-dev-shm-usage",
    "--no-sandbox",
    "--disable-setuid-sandbox",
    "--no-first-run",
    "--no-zygote",
    "--single-process",
  ],
});
const page = await browser.newPage();

try {
  const url = "https://seekingalpha.com/market-news/1";
  const pageOption = {
    waitUntil: "networkidle2",
    timeout: 20000,
  };

  await page.goto(url, pageOption);
} catch (e) {
  console.log(e);
  await page.close();
  await browser.close();
  return resolve("error at 1");
}

try {
  const ulSelector = "#latest-news-list";
  await page.waitForSelector(ulSelector, { timeout: 30000 });
} catch (e) {
  // ALWAYS TIMEOUTS HERE!
  console.log(e);
  await page.close();
  await browser.close();
  return resolve("error at 2");
}
...
Run Code Online (Sandbox Code Playgroud)

Hum*_*der 8

看来问题出在应用程序引擎的内存容量上。

当内存不足以应对傀儡师爬行时,

它会自动生成另一个实例。

但是,新创建的实例具有不同的 puppeteer 浏览器。

因此,结果是Navigation failed because browser has disconnected.

解决方案只是升级应用程序引擎实例,使其可以通过单个实例处理爬行作业。

默认实例是F1,内存为256M,所以我升级到F4,内存为1GB,然后就不再显示错误消息了。

runtime: nodejs12

instance_class: F4

handlers:
  - url: /.*
    secure: always
    script: auto
Run Code Online (Sandbox Code Playgroud)

  • 就一个问题 ?将您提供的代码放在哪里? (4认同)