【发布时间】:2016-06-01 07:30:16
【问题描述】:
我正在使用 while 循环从 csv 文件中打开用户名列表。对于其中的每个用户名,我必须打开一个 URL 并将页面转储到一个文件中。
但是,casper.thenOpen 总是只运行一次。我从Asynchronous Process inside a javascript for loop 了解到这是因为它是一个异步过程。我需要对下面的代码做同样的事情:
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++;
}
});
casper.thenOpen 总是只运行一次,给我这个输出:
current url is first_url
current url is second_url
current url is third_url
I am here
我需要的是这样的
current url is first_url
I am here
current url is second_url
I am here
current url is third_url
I am here
为了让 while 循环正确运行,我正在拔头发!
【问题讨论】:
-
撞!有人在吗?
标签: javascript casperjs