Search code examples
loopsweb-scrapingnightmare

nightmare how to looply select the selector


var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var fs  = require('fs');
var result;
nightmare
        .goto('http://football-system.jp/fss/pub_penaltylist.php?lid=eBVesRz5C54=')
        .wait('select[name="selectTeam"]')
        .evaluate(function () {
         var options = document.querySelectorAll('option'),i;
         var values =[]
         for (i = 1; i < options.length; ++i) {
            values.push(options[i].value)
            }     
            return values;
        })
  .then(function (values) {
    console.log(values)
    values.reduce(function(accumulator, value) {
  return accumulator.then(function(results) {
    return nightmare.goto("http://football-system.jp/fss/pub_penaltylist.php?lid=eBVesRz5C54=")
      .wait('select[name="selectTeam"]')
      .select('select[name="selectTeam"]', value)
      .wait('button[onClick="selectData();"]')
      .click('button[onClick="selectData();"]')
      .wait('table[class="tableOutputDate"]')
      .evaluate(function () {
          return document.querySelector('table[class="tableOutputDate"]').textContent;
        })
      .then(function(result){
        console.log(result)
        results.push(result);
        return results;
      });
  });
}, Promise.resolve([])).then(function(results){

console.log(results)

});


  })
  .catch(function (error) {
    console.error('Search failed:', error);
  });

That is my code .I want to looping select all the selector in that page and get the all html data.I asked a question in here that how to loop in nightmare,but that result cant solve this problem.please help me. Thank you.


Solution

  • I solve my question.the problem is happened in the deal time,not in the loop logic.I add some more wait(time).I think wait(time)is better than the wait(selector).so is over. thank u attention.

    var run = function * () {
      //var values = ['http://www.yahoo.com', 'http://example.com', 'http://w3c.org'];
      var titles = [];
      for (var i = 0; i < values.length; i++) {
        var title = yield nightmare.goto('.............')
          //.wait('select[name="selectTeam"]')
          .wait(2000)
            .select('select[name="selectTeam"]', values[i])
            .wait(2000)
            .click('button[onClick="selectData();"]')
            //.wait('table[class="tableOutputDate"]')
            .wait(2000)
            .evaluate(function () {
              //return document.querySelector('table[class="tableOutputDate"]').textContent;
              var divs = document.querySelectorAll('table[class="tableOutputDate"]'),i;
            var tables = []
            for (i = 0; i < divs.length; ++i) {
                tables.push(divs[i].textContent)
                //result += divs[i].href.toString()+"\n";
                }
            return tables;
            })
        titles.push(title);
      }
      return titles;
    }
    
    vo(run)(function(err, titles) {
      console.dir(titles);
    });