如何告诉CasperJS循环浏览一系列页面 [英] How to tell CasperJS to loop through a series of pages

查看:76
本文介绍了如何告诉CasperJS循环浏览一系列页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试让CasperJS实现以下目标:

I try to make CasperJS achieve the following:


  • 浏览一系列按日期顺序命名的页面。

  • 在每个页面上,找到PDF链接。

  • 下载PDF。

我有一些工作代码,但我不明白CasperJS是如何完成事件序列的。

I got some working code, but I don't understand how CasperJS is going through the sequence of events.

例如,在下面的代码示例中,CasperJS尝试处理第2步,并抛出ReferenceError:找不到变量:formDate,而第1步由于某种原因根本不执行。

For instance, in the code sample below, CasperJS tries to process step 2, and throws a "ReferenceError: Can't find variable: formDate", while step 1 isn't executed at all for some reason.

我的推理出了什么问题?

What's wrong with my reasoning?

在我看来,循环的执行速度不同于 casper.then 方法。

It seems to me that the while loop is executed at a different speed than the casper.then methods.

casper.start();

casper.thenOpen('http://www.example.com', function() {
    this.echo(this.getTitle());
});

casper.then(function() {

    var start = new Date('2013-01-01T00:00:00');
    var end = new Date('2013-01-31T00:00:00');

    while(start < end) {

          // step 1: define formDate  
          casper.then(function() {
            var formDate = start.getFullYear()+"-"+("0" + (start.getMonth() + 1)).slice(-2) +"-"+("0" + start.getDate()).slice(-2) ;
            casper.echo(formDate);

          });

          // Step 2: open the page and download the file
          casper.thenOpen('http://www.example.com/' + formDate, function() {

                        var url = this.getElementAttribute('div#pdffulllink a.pdf', 'href');
                        this.echo(url);
                        this.download(url, 'Downloaded_' + formDate + '.pdf');

          });

          casper.then(function() {
          // Step 3: redefine start
            var newDate = start.setDate(start.getDate() + 1);
            start = new Date(newDate);

          });

    }

});


casper.run(function() {
    this.echo('Done.').exit();
});


推荐答案

经过一番研究,我找到了解决这个问题的方法。

After some research, I found a solution to this problem.

问题是由casper.thenOpen作为异步进程引起的,其余的javascript是同步

The issue is caused by casper.thenOpen being an asynchronous process, and the rest of the javascript being synchronous.

我应用了这个帖子中的优雅方法( javascript for循环中的异步处理)。

I applied an elegant method found in this thread (Asynchronous Process inside a javascript for loop).

遵循该方法,这是一个适用于CasperJS的示例:

Following that method, here is an example that works with CasperJS:

var casper = require('casper').create({
    pageSettings: {
        webSecurityEnabled: false
    }
});

casper.start();

casper.then(function() {
    var current = 1;
    var end = 4;

    for (;current < end;) {

      (function(cntr) {
        casper.thenOpen('http://example.com/page-' + cntr +'.html', function() {
              this.echo('casper.async: '+cntr);
              // here we can download stuff
        });
      })(current);

      current++;

    }

});

casper.run(function() {
    this.echo('Done.').exit();
});

此示例将输出以下内容:

This example will output the following:

casper.async: 1
casper.async: 2
casper.async: 3
Done.

循环正常运行! :)

这篇关于如何告诉CasperJS循环浏览一系列页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆