javascript - CasperJS looping through each URL - Stack Overflow

This question is similar to others, but the problem I had was more basic.This is my code:var links = []

This question is similar to others, but the problem I had was more basic.

This is my code:

var links = [];
var casper = require('casper').create();

function getLinks() {
    var links = document.querySelectorAll('div#mw-content-text table.wikitable tbody tr td i b a');
    return Array.prototype.map.call(links, function(e) {
        return '' + e.getAttribute('href');
    });
}

casper.start('');

casper.then(function() {
    // aggregate results for the 'casperjs' search
    links = this.evaluate(getLinks);
});

casper.each(links, function (self, link) {
    self.thenOpen(fullURL, function () {
        this.echo(this.getTitle() + " - " + link);
    });
});

casper.run();

I know that links get created as it is copied from the Quickstart, but I then modified it to open all the links that were found.

What I'm getting is that nothing is echo'd instead of outputting the each title which is what I expect. This is how I'm calling the file:

~ $ casperjs casper-google-disco.js

This question is similar to others, but the problem I had was more basic.

This is my code:

var links = [];
var casper = require('casper').create();

function getLinks() {
    var links = document.querySelectorAll('div#mw-content-text table.wikitable tbody tr td i b a');
    return Array.prototype.map.call(links, function(e) {
        return 'https://en.wikipedia' + e.getAttribute('href');
    });
}

casper.start('https://en.wikipedia/wiki/David_Bowie_discography');

casper.then(function() {
    // aggregate results for the 'casperjs' search
    links = this.evaluate(getLinks);
});

casper.each(links, function (self, link) {
    self.thenOpen(fullURL, function () {
        this.echo(this.getTitle() + " - " + link);
    });
});

casper.run();

I know that links get created as it is copied from the Quickstart, but I then modified it to open all the links that were found.

What I'm getting is that nothing is echo'd instead of outputting the each title which is what I expect. This is how I'm calling the file:

~ $ casperjs casper-google-disco.js
Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked May 16, 2016 at 9:14 icc97icc97 12.9k9 gold badges83 silver badges97 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The fix was very easy in the end, but took me ages to find it as there were no errors and no-one else seemed to have hit this.

The problem is that the links variable doesn't get set before the each is called. Putting the each inside the then function solves my problem.

The each.js example in the CasperJS samples was helpful for confirming that you can loop through an array without any need for IIFE.

var links = [];
var casper = require('casper').create();

function getLinks() {
    var links = document.querySelectorAll('div#mw-content-text table.wikitable tbody tr td i b a');
    return Array.prototype.map.call(links, function(e) {
        return 'https://en.wikipedia' + e.getAttribute('href');
    });
}

casper.start('https://en.wikipedia/wiki/David_Bowie_discography');

casper.then(function() {
    // aggregate results for the 'casperjs' search
    links = this.evaluate(getLinks);

    casper.each(links, function (self, link) {
        self.thenOpen(link, function () {
            this.echo(this.getTitle() + " - " + link);
        });
    });
});


casper.run();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745328147a4622752.html

相关推荐

  • javascript - CasperJS looping through each URL - Stack Overflow

    This question is similar to others, but the problem I had was more basic.This is my code:var links = []

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信