2017-02-26 61 views
0

我有这样漂亮的代码多个URL,所有我想要做访问之间有些停顿,所以我添加了一个“的setInterval”,但这不工作:访问使用PhantomJS评估错误

var page = require('webpage').create(); 

// the urls to navigate to 
var urls = [ 
    'http://blogger.com/', 
    'https://github.com/', 
    'http://reddit.com/' 
       ]; 
    var i = 0; 

// the recursion function 
var genericCallback = setInterval(function() { 
    return function (status) { 
     console.log("URL: " + urls[i]); 
     console.log("Status: " + status); 
     // exit if there was a problem with the navigation 
     if (!status || status === 'fail') phantom.exit(); 

     i++; 

     if (status === "success") { 

      //-- YOUR STUFF HERE ---------------------- 
      // do your stuff here... I'm taking a picture of the page 
      page.render('example' + i + '.png'); 
      //----------------------------------------- 

      if (i < urls.length) { 

       // navigate to the next url and the callback is this function (recursion) 
       page.open(urls[i], genericCallback()); 

      } else { 
       // try navigate to the next url (it is undefined because it is the last element) so the callback is exit 
       page.open(urls[i], function() { 
        phantom.exit(); 
       }); 
      } 
     } 
    }; 
},2000); 

// start from the first url 
page.open(urls[i], genericCallback()); 

与截图错误我得到: enter image description here 也许有人可以帮助我,并医治此代码?因为我对JS和PhantomJS都很陌生,所以任何帮助都会令人满意。 我从另一个计算器的答案在这里的代码 - Using Multiple page.open in Single Script

+1

'状态'没有被定义....'page.open(url,callback(status));'...该作者给出的脚本通常是不正确的。 –

+0

仍然是相同的错误=(它可能是别的吗? –

+0

但我使用他的代码之前,它的工作现在我编辑setinterval - 它不工作,然后我做你说的所有,并得到相同的错误 –

回答

1

它应该是这样的,我不能给笔者发表评论,因为我没有50美誉:

var page = require('webpage').create(); 

var urls = ['http://blogger.com/','https://github.com/','http://reddit.com/']; 
var i = 0; 

function OpenPage(){ 
    setTimeout(function(){ 
     page.open(urls[i], function(status) { 
      if (status == 'success') { 
        page.render('example' + i + '.png'); 
      } 
      i++; 
      if(i <= urls.length - 1){ 
       OpenPage(); 
      }else{ 
       phantom.exit(); 
      } 
     }); 
    },2000); 
} 

OpenPage(); 
+0

谢谢你队友,这段代码工作起来就像一个魅力! –

+0

亲爱的,Flash迅雷,你能帮我解决这个问题吗? http://stackoverflow.com/questions/42475300/phantomjs-doesnt-resize-the-window –

+0

我已经做了:-) –