Node.JS:带有请求模块的简单 GET 抛出“检测到可能的 EventEmitter 内存泄漏"; [英] Node.JS: Simple GET with request module throwing "Possible EventEmitter memory leak detected"

查看:21
本文介绍了Node.JS:带有请求模块的简单 GET 抛出“检测到可能的 EventEmitter 内存泄漏";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图了解 Web Scraping & 的基础知识用 Python 成功做到了.当尝试模拟相同的 Node 时出现以下错误:

Was trying to understand the basics of Web Scraping & was successful doing it with Python. When trying to emulate the same with Node getting following error :

检测到可能的 EventEmitter 内存泄漏.添加了 11 个管道侦听器.使用emitter.setMaxListeners()增加限制

以下是我尝试使用 node scrape.js 运行的简单代码片段:

Below is simple code snippet that am trying to run using node scrape.js :

const request = require('request');

const uri = 'https://www.somewebsite.com/auth/get_menu/?city_id=1';
const headers = {
    'accept': '*/*',
    'content-type': 'application/json',
    'app_client': 'consumer_web'
};

process.on('warning', e => console.warn(e.stack));

request({uri, headers, method: 'GET'}, (err, response, body) => {
    if(!err){
        console.log(response);

        console.log(body);
    }
});

请告诉我哪里做错了.提前致谢.

Please let me where am doing it wrong. Thanks in advance.

推荐答案

看起来您尝试废弃的网站生成了禁用 cookie 的无限循环,因此您需要在发出请求时启用它们.有人认为这样应该可行.

Looks like the website you are trying to scrap generates infinte loop with cookies disabled, so you need to enable them when make the request. Somethink like this should work.

const request = require("request");

const uri = "https://www.bigbasket.com/auth/get_menu/?city_id=1";
const headers = {
  accept: "*/*",
  "content-type": "application/json",
  app_client: "consumer_web"
};

process.on("warning", e => console.warn(e.stack));

request({ uri, headers, method: "GET", "jar": true }, (err, response, body) => {
  if (!err) {
    console.log(response);

    console.log(err);
  }
});

jar 设置为 true 是这里的诀窍.

setting jar to true is the trick here.

顺便说一下,请求模块不再维护,所以最好使用一些现代包,如 gotaxios 等.希望这有帮助

By the way request module is getting unmaintained so better to use some modern packages like got, axios etc. Hope this helps

这篇关于Node.JS:带有请求模块的简单 GET 抛出“检测到可能的 EventEmitter 内存泄漏";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆