this.echo没有在this.thenOpen中工作

时间:2014-10-03 22:21:55

标签: javascript web-scraping casperjs

我写了一个基于CasperJS - How to open up all links in an array of links的简单CapserJS脚本 并且它不打印标题。

var casper = require('casper').create();
var i = -1;
var links;

casper.start('http://www.waspbarcode.com/barcode-scanners', function() {
  links = ['https://www.yahoo.com'  ,
           'https://www.google.com' ,
           'http://stackoverflow.com']
});

casper.then(function() {
   this.each(links, function() { 
        i++;

        this.thenOpen(links[i], function() {
            this.echo(this.getTitle());
        });
    }); 
    this.exit();
});

casper.run();

1 个答案:

答案 0 :(得分:2)

因为您在尝试打开this.exit();中的网址后尝试了links

您需要了解这是异步工作的。在casperjs开始一个接一个地加载链接后,程序立即退出。