Max*_*ioN 1 javascript node.js puppeteer
我尝试从我的数组中逐页浏览,但得到了这个:
(节点:4196)MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个请求侦听器。使用 Emitter.setMaxListeners() 增加限制(节点:4196) MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。11 帧分离侦听器添加 d。使用 Emitter.setMaxListeners() 增加限制(节点:4196) MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个生命周期事件监听器。使用emitter.setMaxListeners() 增加限制(node:4196) UnhandledPromiseRejectionWarning: Error: Protocol error (Page.navigate): Target closed。at Promise (D:\Kutz\irrParse\node_modules\puppeteer\lib\Connection.js:198:56) at new Promise () at 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) at Array.forEach() at D:\Kutz\irrParse\scrape.js:25:15 at process._tickCallback (internal/process/next_tick.js: 118:7) (node:4196) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。(rjection id: 1) (node:4196) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的承诺拒绝离子将使用非零退出代码终止 Node.js 进程。(节点:4196)未处理的PromiseRejectionWarning:
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();
})();
Run Code Online (Sandbox Code Playgroud)
在 forEach() 中抛出错误
不幸的是,Array.prototype.forEach的迭代器函数并没有像您在将其定义为异步时所期望的那样以异步方式执行。使用 for 循环应该适用于您要执行的操作。
for (let i = 0; i < uniqueLinks.length; i ++) {
await page.goto(uniqueLinks[i], { waitUntil: "load" });
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2733 次 |
| 最近记录: |