迭代增量时Phantomjs

时间:2016-10-28 19:28:03

标签: javascript while-loop phantomjs iteration increment

我有迭代问题,而我必须增加纬度和打印图像的数量,以便自动使用phantomjs制作此脚本。当我启动控制台它不起作用,它没有打印任何东西,可能是错误的迭代,或“phantom.exit”的位置 谁能帮我?谢谢。

n=0;
l=10;  

while(l<22){
var page = require('webpage').create();
page.open("https://www.google.com/maps/preview/@11.483083,-"+l+".145460,10z/data=!3m1!1e3", function(status) {
page.viewportSize = { width: 1440, height: 900 };

page.clipRect = { top: 0, left: 0, width: 250, height: 250 };

  console.log("Status: " + status);
  if(status === "success") {
  n++;
  l++;
  phantom.exit();
    page.render('img-'+n+'.png');
  }
  }); 
};

1 个答案:

答案 0 :(得分:1)

问题解决了,现在它正在运作:

var n=0,l=10;
function to_open(){
var page = require('webpage').create();
page.viewportSize = { width: 1440, height: 900 };
page.clipRect = { top: 0, left: 0, width: 250, height: 250 };
page.open("https://www.google.com/maps/preview/@11.483083,-"+l+".145460,10z/data=!3m1!1e3", function(status) {
  console.log("Status: " + status);
  if(status == "success") {
  n++;
  l++;
  page.render('img-'+n+'.png');
  if(n=="21") {setTimeout(phantom.exit,4000)}else{setTimeout(to_open,4000)}
}else{setTimeout(to_open,2000)}
  });
};to_open();