Oleg  Protsenko
Oleg Protsenko

Reputation: 180

Nightmare and loop method to extract data

I wand to grab all links from site using Nightmare npm. Then go over each link and grab title and text body. I have this code, it works fine when i pass single link, but when i want loop through result of first function i get an error with emmit.setMaxListeners[]... Thanks for any help.

function getUrls1() {
  var n = new Nightmare()
  .goto('http://www.example.com/newslist')
  .evaluate(function () {
    var arr = [];
     $('.news4 dd > a').each(function (i) {
      arr.push({link: $(this).prop('href')})
    })
    return arr;
  }).run(function (err,result) {
    // here i have an array of obj with prop link
    // but i need to loop over each link
    if(err) throw err;
    console.log(result[0].link);
    getText(result[0].link);
  });
}

function getText(link) {
  var n = new Nightmare()
    .goto(link)
    .evaluate(function () {
       return {
         title: $('h1.title').text(),
         text: $('div.text').text()
       };
    }).run(function (err,result) {
      // here i can write file or do something else
      if(err) console.error(err);
      console.log(result);
    })
}

Upvotes: 0

Views: 828

Answers (1)

Ross
Ross

Reputation: 2468

I realize this is an older question, but this was fixed* in PR #407, which was released in a new version shortly after.

* - the event emitter maximum is set to Infinity. See issue #282 for more details.

Upvotes: 1

Related Questions