我试着逐个从数组中走出来,但是得到了以下信息:
(节点:4196) MaxListenersExceededWarning:可能检测到EventEmitter内存泄漏。添加了11个请求侦听器。使用emitter.setMaxListeners()来增加限制(节点:4196) MaxListenersExceededWarning:检测到可能的EventEmitter内存泄漏。11帧分离侦听器adde d.使用emitter.setMaxListeners()增加限制(节点:4196) MaxListenersExceededWarning:检测到可能的EventEmitter内存泄漏。11个生命周期事件侦听器添加ed。使用emitter.setMaxListeners()增加限制(节点:4196) UnhandledPromiseRejectionWarning: UnhandledPromiseRejectionWarning:协议错误(Page.navigate):目标关闭。新承诺() at (D:\Kutz\irrParse\node_modules\puppeteer\lib\Connection.js:198:56) at CDPSession.send (D:\Kutz\irrParse\node_modules\puppeteer\lib\Connection.js:197:12) at CDPSession.send (D:\Kutz\irrParse\node_modules\puppeteer\lib\Page.js:520:39) at Page.goto (D:\Kutz\irrParse\node_modules\puppeteer\lib\Page.js:500:7)在uniqueLinks.forEach (D:\Kutz\irrParse\scrape.js:26:16) at Array.forEach () at D:\Kutz\irrParse\scrape.js:25:15 at process._tickCallback ( promise /process/next_tick.js:118:7)(节点:4196) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误起源于在异步函数中抛出而不带catch块,或者拒绝使用.catch()处理的承诺。(r射血id: 1) (节点:4196) DEP0018 DeprecationWarning:未处理的承诺拒绝被拒绝。将来,承诺拒绝未处理的离子将用非零退出代码终止Node.js进程。(节点:4196) UnhandledPromiseRejectionWarning: UnhandledPromiseRejectionWarning:超出导航超时:超过30000ms在Promise.then
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()中的错误抛出
发布于 2018-04-12 02:52:18
不幸的是,Array.prototype.forEach的迭代器函数在定义为异步时并没有像您所期望的那样以异步方式执行。使用for循环应该适用于您想要做的事情。
for (let i = 0; i < uniqueLinks.length; i ++) {
await page.goto(uniqueLinks[i], { waitUntil: "load" });
}https://stackoverflow.com/questions/49381247
复制相似问题