Hi,
I'm trying to .push several urls to a global variable inside a function of a javascript and casperjs script. The global variable does not seem to be filled with the data.
var page_links = ['http://www.olx.pt'];
var product_links = [];
casper.start();
function spider(url) {
casper.open(url).then(function() {
var pager = this.evaluate(getMorePages);
// get the product_links of the webpage
var temp_links = this.evaluate(getLinks);
for (j = 0; j < temp_links.length; ++j) {
product_links.push(temp_links[j]);
this.echo("temp_link: " + temp_links[j]);
this.echo("product pushed: " + product_links[product_links.length - 1]);
}
for (var i = 0; i < pager.length; ++i) {
var new_url = pager[i];
if (! existsInArray(new_url, page_links)) {
page_links.push(new_url);
this.echo("page link pushed: " + new_url);
spider(new_url);
}
}
});
casper.then(function() {
casper.echo("pl0: ", product_links.join('\n .. '));
casper.echo("pl0.length: ", product_links.length);
});
}
casper.then(function() {
spider(page_links[0]);
});
So, I'm trying to push product urls to the product_links
global variable and it seems to be working, as far as the two echo statements show:
this.echo("temp_link: " + temp_links[j]);
this.echo("product pushed: " + product_links[product_links.length - 1]);
But, in another step, when I try to access product_links
, in the next casper.next
step, product_links
seems to be empty.
How to correct this?