CasperJS - 循环遍历url数组

时间:2018-04-11 08:18:41

标签: javascript loops url casperjs

我正在尝试使用CasperJS遍历一个url数组并执行几个步骤。

var links = ['https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929',
'https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876'];

但是当我使用evaluate函数时,return变量为null或仅适用于第一个URL:

    casper.start().each(links, function(self, link) {
        self.thenOpen(link, function() {
            var list = this.evaluate(function(){
                return document.getElementsByClassName("_50u0 _60p- _14hj")
            });
        console.log("This page contains :", list.length, " unanswered comments");
    });
});

感谢您的回答。

2 个答案:

答案 0 :(得分:0)

我尝试了你的代码,它对我来说运行正常,这是一个有点用的版本供你尝试:

var casper = require('casper').create({
  logLevel: 'debug',
  verbose: true,
  viewportSize: {width: 1200, height: 1080 }
});

var links = [
  'https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929',
  'https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876'
];

casper
  .start()
  .each(links, function (self, link) {
    self.thenOpen(link, function () {
      var list = this.evaluate(function () {
        return document.getElementsByClassName("_50u0 _60p- _14hj")
      });
      console.log("This page contains :", list.length, " unanswered comments");
  });
})
.run();

这是我的输出:

[info] [phantom] Starting...
[info] [phantom] Running suite: 4 steps
[debug] [phantom] opening url: https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929, HTTP GET
[debug] [phantom] Navigation requested: url=https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929"
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/4 https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929 (HTTP 200)
This page contains : 0  unanswered comments
[info] [phantom] Step anonymous 2/4: done in 2670ms.
[debug] [phantom] opening url: https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876, HTTP GET
[debug] [phantom] Navigation requested: url=https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 4/4 https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876 (HTTP 200)
This page contains : 0  unanswered comments
[info] [phantom] Step anonymous 4/4: done in 3524ms.
[info] [phantom] Done 4 steps in 3547ms
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

显然会为你的数组中的两个链接运行你的代码。

答案 1 :(得分:0)

您可以使用each()循环浏览每个页面,getElementsInfo()可以检索有关与提供的选择器匹配的所有元素的信息。

这将允许您避免在CasperJS环境和远程DOM环境之间切换。您可以从documentation了解有关不同环境的更多信息。

简单解决方案:

var links = [
  'https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929',
  'https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876'
];

var casper = require('casper').create();

casper.each(links, function (self, link) {
  self.thenOpen(link);

  self.then(function () {
    var list = this.getElementsInfo('._50u0._60p-._14hj');

    this.echo('This page contains: ' + list.length + ' unanswered comments');
  });
});

casper.run();
  

注意:此方法不返回NodeList,只返回匹配元素的简单对象表示数组;这是因为Casper环境和页面DOM环境不一样,所以需要对DOM对象进行序列化。