2016-11-21 83 views
0
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); 
    }); 

這是我的代碼。我想循環選擇該頁面中的所有選擇,並得到了所有的HTML data.I在這裏如何循環的噩夢問了一個問題,但那結果不能解決這個問題。請幫助我。謝謝。噩夢如何looply選擇選擇

回答

1

我解決了我的問題。問題發生在處理時間,而不是在邏輯邏輯。我添加了一些更多的等待(時間)。我認爲等待(時間)比等待(選擇器).so更好過度。謝謝你的關注。

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); 
});