How to tell CasperJS to loop through a series of pages

前端 未结 1 1866
眼角桃花
眼角桃花 2020-12-28 19:47

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.
相关标签:
1条回答
  • 2020-12-28 20:43

    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! :)

    0 讨论(0)
提交回复
热议问题