循环以使用PhantomJS打开WebPage(每个循环迭代具有不同的参数)不起作用

时间:2017-03-27 08:18:38

标签: javascript phantomjs

当我尝试使用以下循环打开页面时:

      console.log("Just before time, endTime and loopnumber gets declared.");
      var time, endTime;
      var loopnumber = 6;
      console.log("time declared as '"+time+"', loopnumber as '"+ loopnumber+"'");
      for (time = 1490543999999999, endTime = time - 518400000000 * 10; time > endTime; time -= 518400000000) {
        console.log("inside loop. time declared as '"+time+"', loopnumber as '"+ loopnumber+"'");
        var mintime = time - 518399999999;

        page.open('https://myactivity.google.com/item?min='+mintime+'&max='+time+'&product=5,6,7,10,11,12,13,15,19,20,25,27,28', function () {
        console.log("inside callback");
        page.render('/Users/jMac-NEW/Documents/FILEMAKER OCLC/login_test33.1_brkpoint'+loopnumber+'.png');
        fs.write('/Users/jMac-NEW/Documents/FILEMAKER OCLC/login_test33.1_brkpoint'+loopnumber+'html.html',page.content,'w');

      });
      loopnumber++;
      }

console.log("inside loop. time declared as '"+time+"', loopnumber as '"+ loopnumber+"'");被调用,但不是console.log的回调函数内的page.openpage.renderfs.write也不起作用(我在目录中看不到预期的结果文件。)

怎么回事?

编辑: 如果只是因为page.open没有足够的时间,我怎么能给它足够的时间?与setInterval()

EDIT2: 我试图让代码做的是为循环的每次迭代用不同的参数集调用myactivity.google.com/item,将网页保存为.png文件和.html文件

是的,我在此代码段之前导入了fs,只是它没有在此处显示。

1 个答案:

答案 0 :(得分:1)

也许最简单的方法是通过上一页加载的回调触发下一页加载。这样你知道上一页已经完成加载(注意,加载并不一定意味着呈现,如果有&# 39;在你的页面上运行一堆Javascript来渲染某些东西 - 例如图表,你仍然可以在它完成之前渲染你的屏幕

这样的事情:

var time = 1490543999999999;
var endTime = time - 518400000000 * 10;
var mintime = time - 518399999999;

page.open('https://myactivity.google.com/item?min=' + mintime + '&max=' + time + '&product=5,6,7,10,11,12,13,15,19,20,25,27,28', callback);

function callback() {
  console.log("inside callback");
  page.render('/Users/jMac-NEW/Documents/FILEMAKER OCLC/login_test33.1_brkpoint' + loopnumber + '.png');
  fs.write('/Users/jMac-NEW/Documents/FILEMAKER OCLC/login_test33.1_brkpoint' + loopnumber + 'html.html', page.content, 'w');
  time -= 518400000000;
  if (time > endTime) {
    var mintime = time - 518399999999;
    page.open('https://myactivity.google.com/item?min=' + mintime + '&max=' + time + '&product=5,6,7,10,11,12,13,15,19,20,25,27,28', callback);
  }
}