CasperJS - 如何打开链接数组中的所有链接

Mic*_*ski 8 html javascript phantomjs casperjs

我试图让CasperJS打开链接中的每个array链接.我有它,以便在我打开链接后,它将显示该页面的标题.然而,当我运行它时,没有显示任何内容.

我可以用a for loop来显示链接,它完美无缺.

这是我刚才解释的代码:

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();
Run Code Online (Sandbox Code Playgroud)

这是我尝试的另一种方法:

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();
Run Code Online (Sandbox Code Playgroud)

它说未定义的'x'.请注意,我将x设置为全局变量.你可以做的任何修改都会很棒.谢谢.

Mic*_*ski 8

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();
Run Code Online (Sandbox Code Playgroud)


小智 8

var i = 0;
var nTimes = x.length;

casper.repeat(nTimes, function() {
    //... do your stuff
    i++;
});
Run Code Online (Sandbox Code Playgroud)

为我工作.


abd*_*del 5

casper.start('about:blank');

var urls = ['http://google.fr', 'http://yahoo.fr', 'http://amazon.fr'];

casper.each(urls, function(casper, url) {
  casper.thenOpen(url, function() {
        this.echo("I'm in your " + url + ".");
    });
});
Run Code Online (Sandbox Code Playgroud)