CasperJS: Iterating through URL's

前端 未结 2 671
灰色年华
灰色年华 2020-12-18 10:36

I\'m pretty new to CasperJS, but isn\'t there a way to open a URL and execute CasperJS commands in for loops? For example, this code doesn\'t work as I expected it to:

2条回答
  •  梦毁少年i
    2020-12-18 11:01

    If you need to get context then use the example here: https://groups.google.com/forum/#!topic/casperjs/n_zXlxiPMtk

    I used the IIFE (immediately-invoked-function-expression) option.

    Eg:

    for(var i in links) {
      var link = links[i];
    
      (function(index) {
        var link = links[index]
        var filename = link.replace(/#/, '');
        filename = filename.replace(/\//g, '-') + '.png';
    
        casper.echo('Attempting to capture: '+link);
        casper.thenOpen(vars.domain + link).waitForSelector('.title h1', function () {
          this.capture(filename);
        });
      })(i);
    }
    

    links could be an array of objects and therefore your index is a reference to a group of properties if need be...

    var links = [{'page':'some-page.html', 'filename':'page-page.png'}, {...}]
    

提交回复
热议问题