CasperJS循环加载下一页

CasperJS Load next page in loop

本文关键字:一页 加载 循环 CasperJS      更新时间:2023-09-26

我一直在研究一个脚本,该脚本整理了来自网站的用户列表的分数。一个问题是,我正在尝试在 while 循环中加载下一页,但该函数没有加载......

casper.then(function () {
    var fs = require('fs');
    json = require('usernames.json');
    var length = json.username.length;
    leaderboard = {};
    for (var ii = 0; ii < length; ii++) {
        var currentName = json.username[ii];
        this.thenOpen("http://www.url.com?ul=" + currentName + "&sortdir=desc&sort=lastfound", function (id) {
                return function () {
                    this.capture("Screenshots/" + json.username[id] + ".png");
                    if (!casper.exists(x("//*[contains(text(), 'That username does not exist in the system')]"))) {
                        if (casper.exists(x('//*[@id="ctl00_ContentBody_ResultsPanel"]/table[2]'))) {
                            this.thenEvaluate(tgsagc.tagNextLink);
                            tgsagc.cacheCount = 0;
                            tgsagc.
                            continue = true;
                            this.echo("------------ " + json.username[id] + " ------------");
                            while (tgsagc.
                                continue) {
                                this.then(function () {
                                    this.evaluate(tgsagc.tagNextLink);
                                    var findDates, pageNumber;
                                    pageNumber = this.evaluate(tgsagc.pageNumber);
                                    findDates = this.evaluate(tgsagc.getFindDates);
                                    this.echo("Found " + findDates.length + " on page " + pageNumber);
                                    tgsagc.checkFinds(findDates);
                                    this.echo(tgsagc.cacheCount + " Caches for " + json.username[id]);
                                    this.echo("Continue? " + tgsagc["continue"]);
                                    this.click("#tgsagc-link-next");

                                });
                            }
                            leaderboard[json.username[id]] = tgsagc.cacheCount;
                            console.log("Final Count: " + leaderboard[json.username[id]]);
                            console.log(JSON.stringify(leaderboard));
                        } else {
                            this.echo("------------ " + json.username[id] + " ------------");
                            this.echo("0 Caches Found");
                            leaderboard[json.username[id]] = 0;
                            console.log(JSON.stringify(leaderboard));
                        }

                    } else {
                        this.echo("------------ " + json.username[id] + " ------------");
                        this.echo("No User found with that Username");
                        leaderboard[json.username[id]] = null;
                        console.log(JSON.stringify(leaderboard));
                    }
                });
while (tgsagc.continue) {
    this.then(function(){
        this.evaluate(tgsagc.tagNextLink);
        var findDates, pageNumber;
        pageNumber = this.evaluate(tgsagc.pageNumber);
        findDates = this.evaluate(tgsagc.getFindDates);
        this.echo("Found " + findDates.length + " on page " + pageNumber);
        tgsagc.checkFinds(findDates);
        this.echo(tgsagc.cacheCount + " Caches for " + json.username[id]);
        this.echo("Continue? " + tgsagc["continue"]);
        return this.click("#tgsagc-link-next");
    });
}

好的,看看这段代码,我可以建议你应该做一些改变:

  1. 我认为您不应该在 then() 中从函数中调用返回。这可能会过早终止函数。查看 casperjs 文档,这些示例也没有返回任何内容。
  2. 在你的while循环中,是什么将"tgsagc.continue"设置为false?
  3. 不要使用"继续"作为变量名称。它是 Javascript 中的一个保留字,用于终止循环的迭代。在您的情况下,这应该不是问题,但无论如何都是不好的做法。
  4. 不要在对 then() 函数的调用中不断重新定义方法。重构代码,以便在其他位置定义一次代码。

我们最终不得不限定函数的范围,因此它会在循环中加载下一页。

这主要是因为CasperJS不是为计算分数而设计的,它试图异步进行计算,缺少所需的函数