I'm using a while loop to open a list of usernames from a csv file. For each username of these, I have to open a URL and dump the page into a file.
However, then casper.thenOpen always runs only one time. I understood from Asynchronous Process inside a javascript for loop that this is due to that it is an asynchronous process. I need to do the same for my code below:
casper.then(function(){
stream = fs.open('usernames.csv', 'r');
targetusername = stream.readLine();
i = 0;
while(targetusername) {
var url = "http://blablalb" + targetusername;
console.log("current url is " + url);
casper.thenOpen(url, function() {
console.log ("I am here");
fs.write(targetusername,this.getTitle() + "\n",'w');
fs.write(targetusername,this.page.plainText,'a');
});
targetusername = stream.readLine();
i++;
}
});
The casper.thenOpen always runs only one time, giving me this output:
current url is first_url
current url is second_url
current url is third_url
I am here
What I need is like this
current url is first_url
I am here
current url is second_url
I am here
current url is third_url
I am here
I'm pulling my hair out to get that while loop running correctly!
I could reach the exact output that I needed:
current url is first_url
I am here
current url is second_url
I am here
current url is third_url
I am here
using the repeat function, as follows:
casper.then(function(){
stream = fs.open('usernames.csv', 'r');
casper.repeat(3, function() {
targetusername = stream.readLine();
var url = "http://blablalb" + targetusername;
console.log("current url is " + url);
casper.thenOpen(url, function() {
console.log ("I am here");
fs.write(targetusername,this.getTitle() + "\n",'w');
fs.write(targetusername,this.page.plainText,'a');
});
}
)});