【问题标题】:Node js Puppeteer goto array of pagesNode js Puppeteer goto 页面数组
【发布时间】:2018-08-29 02:12:48
【问题描述】:

我尝试从我的数组中逐页浏览,但得到了这个:

(node:4196) MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个请求侦听器。采用 发射器.setMaxListeners() 增加限制 (节点:4196) MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个框架分离的侦听器 d。使用emitter.setMaxListeners() 增加限制 (节点:4196) MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加 11 个生命周期事件监听器 编。使用emitter.setMaxListeners() 增加限制 (节点:4196)UnhandledPromiseRejectionWarning:错误:协议错误(Page.navigate):目标已关闭。 在 Promise (D:\Kutz\irrParse\node_modules\puppeteer\lib\Connection.js:198:56) 在新的承诺 () 在 CDPSession.send (D:\Kutz\irrParse\node_modules\puppeteer\lib\Connection.js:197:12) 在导航 (D:\Kutz\irrParse\node_modules\puppeteer\lib\Page.js:520:39) 在 Page.goto (D:\Kutz\irrParse\node_modules\puppeteer\lib\Page.js:500:7) 在 uniqueLinks.forEach (D:\Kutz\irrParse\scrape.js:26:16) 在 Array.forEach () 在 D:\Kutz\irrParse\scrape.js:25:15 在 在 process._tickCallback (internal/process/next_tick.js:118:7) (节点:4196) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于抛出 在没有 catch 块的异步函数内部,或者通过拒绝未使用 .catch() 处理的承诺。 (r 弹射编号:1) (节点:4196)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。未来,promise 拒绝 未处理的离子将使用非零退出代码终止 Node.js 进程。 (节点:4196)UnhandledPromiseRejectionWarning:错误:超过导航超时:超过 30000 毫秒 在 Promise.then (D:\Kutz\irrParse\node_modules\puppeteer\lib\NavigatorWatcher.js:71:21) 在

const puppeteer = require("puppeteer");
var forEach = require('async-foreach').forEach;


const url = "https://reddit.com/r/programming";
const linkSelector = ".content a.title";

(async () => {
  // Launch chrome process
  const browser = await puppeteer.launch({headless: true});
  const page = await browser.newPage();

  await page.goto(url, { waitUntil: "load" });

  // This runs the `document.querySelectorAll` within the page and passes
  // the result to function
  const links = await page.$$eval(linkSelector, links => {
    return links.map((link) => link.href);
  });

  // Make sure we get the unique set of links only
  const uniqueLinks = [...links];
  //console.log(uniqueLinks[0]);

  uniqueLinks.forEach(async (link) => {
    await page.goto(link, { waitUntil: "load" });
  });

  // Kill the browser process
  await browser.close();
})();

forEach() 中抛出错误

【问题讨论】:

标签: javascript node.js puppeteer


【解决方案1】:

不幸的是,Array.prototype.forEach 的迭代器函数并未像您将其定义为异步时所期望的那样以异步方式执行。使用 for 循环应该适用于您正在尝试做的事情。

for (let i = 0; i < uniqueLinks.length; i ++) {
  await page.goto(uniqueLinks[i], { waitUntil: "load" });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 2019-09-30
    • 1970-01-01
    • 2019-07-04
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多