在phantomJS中循环方法?

时间:2016-08-08 18:04:29

标签: javascript callback phantomjs webpage-screenshot

我正在尝试自动下载许多网页截图的过程。所有屏幕截图都在同一页面上,但根据查询字符串参数显示不同的数据。

我的代码:

  var page = require('webpage').create();
  //How far the page extends in the image
  page.viewportSize = { width: 1024, height: 2500 };

  //size of the actual clip in the viewer (extra black space below image)
  page.clipRect = { top: 0, left: 0, width: 1024, height: 4500 };
console.log("configured");




for(var i = 1; i < 5; i++){


  page.open('http://testsite.com/view?id=' + i, function(status) {
    console.log("Status: " + status);
    if(status === "success") {

      //save image of page as this
      page.render(i + '.png');

    }
phantom.exit();


  });
}

这没有for循环,只是将i设置为随机数。我有一种感觉它与回调函数和/或phantom.exit()的位置有关。我尝试了很多变化,无法让它循环。我也尝试过添加自定义同步延迟功能,但我不确定这可能会被调用。什么是自动执行此操作的最佳方式...时间和资源不是一个密集的问题,因为这只需要运行一次就可以将所有图片转储到文件中。

0 个答案:

没有答案