Search code examples
web-scrapingphantomjscasperjs

Open multiple links in casperjs


I am trying to scrape all links of special kind (boxscore-links) from this website http://www.basketball-reference.com/teams/GSW/2016_games.html and then visit them one by one, scraping some information from every visited link. For a beginning I want to scrape all links, visit them one by one and get a title of website. The problem is that it always prints the same title and the same current url (initial url) even though it clearly has to be a new one. Seems to me that there is a problem with 'this'-keyword... (Don't look at limit of links, I took the code from sample on github of casperjs and I left it for console not to be overloaded.) This is my code:

var casper = require("casper").create({
    verbose: true
});

// The base links array
var links = [ "http://www.basketball-reference.com/teams/GSW/2016_games.html" ];

// If we don't set a limit, it could go on forever
var upTo = ~~casper.cli.get(0) || 10;
var currentLink = 0;

// Get the links, and add them to the links array
function addLinks(link) {
    this.then(function() {
        var found = this.evaluate(searchLinks);
        this.echo(found.length + " links found on " + link);
        links = links.concat(found);
    });
}

// Fetch all <a> elements from the page and return
// the ones which contains a href starting with 'http://'

function searchLinks() {
    var links = document.querySelectorAll('#teams_games td:nth-child(5) a');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href');
    });
}

// Just opens the page and prints the title
function start(link) {
    this.start(link, function() {
        this.wait(5000, function() {
            this.echo('Page title: ' + this.getTitle());
            this.echo('Current url: ' + this.getCurrentUrl());
        });
    });
}

// As long as it has a next link, and is under the maximum limit, will keep running
function check() {
    if (links[currentLink] && currentLink < upTo) {
        this.echo('--- Link ' + currentLink + ' ---');
        start.call(this, links[currentLink]);
        addLinks.call(this, links[currentLink]);
        currentLink++;
        this.run(check);
    } else {
        this.echo("All done.");
        this.exit();
    }
}

casper.start().then(function() {
    this.echo("Starting");
});

casper.run(check);

Solution

  • Considering an array of URLs, you can iterate over them, visiting each in succession with something like the following:

    casper.each(urls, function(self, url) {
        self.thenOpen(url, function(){
            this.echo('Opening: ' + url);
            // Do Whatever
        });
    });
    

    Obviously this will not find links on a page, but it is a nice way to go over a known set of URLs.