【发布时间】: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() 中抛出错误
【问题讨论】:
-
你解决了这个问题吗?今天也解决了这个问题。
-
与
Array.prototype.forEach无关,这里回答:stackoverflow.com/questions/9768444/… -
这能回答你的问题吗? Using async/await with a forEach loop
标签: javascript node.js puppeteer