Casperjs iterating over a list of links using casper.each

前端 未结 3 958
星月不相逢
星月不相逢 2021-01-12 02:15

I am trying to use Casperjs to get a list of links from a page, then open each of those links, and add to an array object a particular type of data from those pages.

3条回答
  •  孤街浪徒
    2021-01-12 02:55

    If I understand your problem correctly, to solve, give items[] a global scope. In your code, I would have done the following:

    var items = [];
    this.each(listOfLinks, function(self, link) {
    
        var eachPageHref = link.href;
    
        console.log("Creating new array in object for " + eachPageHref);
    
        object[date][eachPageHref] = []; // array for page to store names
    
        self.thenOpen(eachPageHref, function () {
    
            this.evaluate(function() {
            // Perform DOM manipulation to get items
            items.push(whateverThisItemIs);
          });
        });
    

    Hope this helps.

提交回复
热议问题