I'm using nightmare.js to scrape webpage content.
After authenticating the nightmare instance, I loop through multiple pages (that require a login) and then call the run method that executes all the page loads.
From each page I want to grab specific content so I call the evaluate function which ensures we're executing inside the browser scope. Whatever is returned from the evaluate function becomes the argument in the run method
But I'm trying to run the evaluate function multiple times (once for each page). But the evaluate function can only return output to the run method once. I tried pushing content to a global variable (in the main scope) but can't access it from inside the browser scope.
Can anyone advise on how to have multiple evaluate methods run on one nightmare instance and extract information from each?
var Nightmare = require("nightmare");
//Creates the authenticated nightmare instance
var scraper = new Nightmare()
.goto('https://www.example.com/signin')
.type('#login', 'username')
.type('#password', 'password')
.click('#btn')
.run(function(err, nightmare) {
if (err) {
console.log(err);
}
console.log('Done.');
});
for (var i = 0; i < 4; i++) {
scraper
.goto('https://www.example.com/page'+i)
.wait(1000)
.evaluate(function(){
return $('#result > h3').text()
})
}
scraper.run(function(err, result) {
console.log(result)
if (err) {
console.log(err);
}
}); )