How to tell CasperJS to loop through a series of pages

不问归期 提交于 2019-12-30 00:36:08

问题


I try to make CasperJS achieve the following:

  • Go through a series of pages that are named sequentially by date.
  • On each page, locate a PDF link.
  • Download the PDF.

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

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?

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

回答1:


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

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

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

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.

The loop is working! :)



来源:https://stackoverflow.com/questions/28421307/how-to-tell-casperjs-to-loop-through-a-series-of-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!