【问题标题】:Parsing multiple pages of website and count total items解析网站的多个页面并计算总项目数
【发布时间】:2015-02-07 04:51:00
【问题描述】:

我的脚本只是收集一页上的报告数量,然后转到下一页并执行相同的操作。目标是获取跨多个页面的报告总数。

更新

var casper = require('casper').create({
    clientScripts: ["./lib/jquery-2.1.3.min.js"],
    // verbose: true,
    logLevel: "debug"
});

casper.on('remote.message', function(msg) {
    this.echo('LOG: ' + msg);
});

casper.on('page.error', function (msg, trace) {
    this.echo( 'Error: ' + msg, 'ERROR' );
});

var reportCount, newReportCount, reportPages;

casper.start("reports.html", function() {

    reportPages = this.evaluate(function() {
        return $('#table2 tbody tr td').children('a').length -1;
  });

  //first page of reports
  reportCount = this.evaluate(function() {
      return $('#table1 tbody').first().children('tr').length;
  });

  this.echo('initial count: ' + reportCount);
  this.echo('pages: ' + reportPages);

  //check if more than 1 page and add report count
  if (reportPages > 1) {
    newReportCount = this.thenOpen('reports2.html', function(){
        var newCount = this.evaluate(function(count) {
            add = count + $('#table1 tbody').first().children('tr').length;
            // console.log('new count inside: ' + add);
            return add;
        }, reportCount);
        console.log(newCount); //this shows correct new value 32
    });
    console.log(newReportCount); //this shows [object Casper]

    neoReportCount = this.thenOpen('reports3.html', function(count){
        console.log(newReportCount); //this shows [object Casper]
        //do the same count
    }, newReportCount);
  }

casper.run();

这是控制台的输出

页数:3 第一次计数:15 [object Casper],当前位于 file:///**/reports.html 32 [object Casper],当前位于 file:///**/reports3.html

【问题讨论】:

    标签: javascript casperjs


    【解决方案1】:

    是的,这是可能的,但您使用 casper.thenOpenAndEvaluate(),其中包含单词 then。这意味着此函数是异步的,它返回casper 对象以启用构建器/承诺模式。所以你不能从这样的函数返回任何东西。由于是异步的,所以会在当前步骤结束后执行,也就是console.log(newCount);之后。

    你需要拆分函数,例如这样:

    //check if more than 1 page and add report count
    if (reportPages > 1) {
      var newCount;
      this.thenOpen('reports2.html', function(count){
        newCount = this.evaluate(function(count){
          add = count + $('#table1 tbody').first().children('tr').length;
          console.log('new count inside: ' + add);
          return add;
        }, reportCount);
        console.log(newCount);
      }).thenOpen('reports3.html', function(count){
        newCount += this.evaluate(function(count){
          add = count + $('#table1 tbody').first().children('tr').length;
          console.log('new count inside: ' + add);
          return add;
        }, reportCount);
        console.log(newCount);
      }).then(function(){
        console.log(newCount);
      });
    }
    

    您似乎想要遍历多个页面。这通常是递归完成的,因为 CasperJS 是异步的,并且您事先不知道需要打开多少页面。我建议你看看这个问题的一些例子:CasperJS loop or iterate through multiple web pages?

    【讨论】:

    • 你好。感谢您的答复。我根据您提出的分离打开和评估的建议修改了代码。我仍然无法弄清楚如何在 .thenOpen 函数之外计算新的计数值。 newReportCount 的日志看起来像是在返回值之前执行的,所以这就是我获取对象的原因?我想我需要某种回调,但我将如何在 casper 中实现呢?
    • @JeffreyTu 请仔细阅读我的回答。您不能从 casper.thenOpen 返回某些内容,因为它与所有其他 then*wait* 函数一样是异步的。您可能希望 newCount 是一个全局变量,以便您可以添加到它?
    猜你喜欢
    • 1970-01-01
    • 2022-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2015-07-10
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多