【问题标题】:Open multiple links in casperjs在 casperjs 中打开多个链接
【发布时间】:2016-01-27 11:42:47
【问题描述】:

我正在尝试从该网站http://www.basketball-reference.com/teams/GSW/2016_games.html 抓取所有特殊类型的链接(boxscore-links),然后逐个访问它们,从每个访问的链接中抓取一些信息。首先,我想抓取所有链接,逐个访问它们并获得网站标题。问题是它总是打印相同的标题和相同的当前 url(初始 url),即使它显然必须是一个新的。在我看来,“this”关键字有问题...... (不要看链接的限制,我从 casperjs 的 github 上的示例中获取了代码,并将其留给控制台不要超载。) 这是我的代码:

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);

【问题讨论】:

标签: web-scraping phantomjs casperjs


【解决方案1】:

考虑到一个 URL 数组,您可以遍历它们,依次访问每个 URL,如下所示:

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

显然这不会在页面上找到链接,但它是遍历一组已知 URL 的好方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2016-09-27
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    相关资源
    最近更新 更多