我有 3 个 URL 需要循环查找要下载的文件。
lst = ["BX", "BL", "BM"];
lst.forEach(function(i){
var url = 'http://www.tzg-infocenter.com/visubl_php//images/' + i + '/' + clip + '/' + casper.cli.raw.get('entryDate') + '/' + ref + '.PDF';
casper.start(url,function(){
this.echo('>>> Starting URL ' + url);
}
});
casper.run();
但只有最后一个元素被回显。 “起始 URL...”仅回显一次。为什么 Casper 不迭代整个数组?
最佳答案
看起来您正在每次迭代中重新启动 casper。而不是在第一次迭代后使用 thenOpen
。
Signature: thenOpen(String location[, mixed options])
Adds a new navigation step for opening a new location, and optionally add a next step when its loaded:...
尝试以下操作:
var casper = require('casper').create();
var urls = ['https://google.com', 'http://casperjs.org/', 'http://phantomjs.org']
urls.forEach(function(url, idx) {
if ( idx > 0 ) {
casper.thenOpen( url, function() {
this.echo(this.getTitle());
});
} else {
casper.start( url, function() {
this.echo(this.getTitle());
});
}
})
casper.run();
关于javascript - 让 CasperJS 迭代 URL 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43911237/