I want to scrapy and get the html by each herf in the page,then output to a csv.now I first time to use nightmare.so I have a problem about parament not defined.
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var fs = require('fs');
var result ;
nightmare
.goto('http://football-system.jp/fss/pub_kaijyolist.php?lid=h0xYuxqKQ+M=')
.wait(1000)
.evaluate(function () {
var divs = document.querySelectorAll('a[target="_blank"]'),i;
for (i = 0,result = ""; i < divs.length; ++i) {
result += divs[i].href.toString()+"\n";
}
return divs;
})
.end()
.then(function (divs) {
console.log(divs)
///////////////////////////////////////////////////
fs.writeFile('8.csv', result, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
////////////////////////////////////////////////////
})
.then(function() {
//since Nightmare has an internal `.then()`, return the instance returned by the final call in the chain
return nightmare
//click the next button to get the next page of search results
.goto(divs[0].href)
//get the first HREF from the second page of results
.evaluate(function() {
return document.querySelector('div[class="outputDate"]');
})
})
.catch(function (error) {
console.error('Search failed:', error);
});
The second goto() have a Error: divs is not defined. Thanks a lot.