CasperJS - 如何打开链接数组中的所有链接 [英] CasperJS - How to open up all links in an array of links
问题描述
我试图让CasperJS打开链接的数组
中的每个链接。我拥有它,因此在打开链接后,它将显示该页面的标题。但是当我运行它时,什么都不显示。
我可以使用 for循环
来显示链接,它完美地工作。
这是我刚刚解释的代码:
pre $ var x;
casper.start(URL,function(){
x = links.split(); //现在x是一个链接数组
for(var i = 0; j< x.length; i ++)//对于每个链接...
{
casper.thenOpen(partialURL + x [i],function(){ //打开该链接
console.log(this.getTitle()+'\\\
'); //显示页面标题
});
}
this.exit();
});
casper.run();
这是我尝试过的另一种方法:
var x;
casper.start(URL,function(){
x = links.split(); //现在x是一个链接数组
this.exit();
});
for(var i = 0; j< x.length; i ++)//对于每个链接...
{
casper.thenOpen(partialURL + x [i ],function(){//打开链接
console.log(this.getTitle()+'\\\
'); //显示页面标题
});
}
casper.run();
它表示'x'未定义。请注意,我将x设置为全局变量。
你可以做的任何修改都会很棒。谢谢。
var x; var i = -1;
casper.start(URL,function(){
x = links.split(); //现在x是一个链接数组
});
casper.then(function(){
this.each(x,function(){
++++; //改变正在打开的链接(必须在这里具体说明)
this.thenOpen((partialURL + x [i]),function(){
this.echo(this.getTitle()); //显示页面标题
});
});
});
casper.run();
I'm trying to make it so that CasperJS will open up every link in an array
of links. I have it so that after I open a link, it will display the title of that page. Yet when I run it, nothing is displayed.
I can use a for loop
to display the links and it works perfectly.
This is the code for what I just explained:
var x;
casper.start(URL, function() {
x = links.split(" "); // now x is an array of links
for (var i = 0; j < x.length; i++) // for every link...
{
casper.thenOpen(partialURL + x[i], function() { // open that link
console.log(this.getTitle() + '\n'); // display the title of page
});
}
this.exit();
});
casper.run();
This is another method I tried:
var x;
casper.start(URL, function() {
x = links.split(" "); // now x is an array of links
this.exit();
});
for (var i = 0; j < x.length; i++) // for every link...
{
casper.thenOpen(partialURL + x[i], function() { // open that link
console.log(this.getTitle() + '\n'); // display the title of page
});
}
casper.run();
It says that 'x' in undefined. Notice that I set x to be a global variable though. Any modifications that you could make would be great. Thanks.
var x; var i = -1;
casper.start(URL, function() {
x = links.split(" "); // now x is an array of links
});
casper.then(function() {
this.each(x, function() {
i++; // change the link being opened (has to be here specifically)
this.thenOpen((partialURL + x[i]), function() {
this.echo(this.getTitle()); // display the title of page
});
});
});
casper.run();
这篇关于CasperJS - 如何打开链接数组中的所有链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!