2015-09-01 76 views
2

我試圖讓我的代碼循環訪問一個url數組,但陷入困境。使用javascript在phantomjs中通過一系列網址循環使用javascript

這段代碼只是在phantomjs中運行並輸出請求的url和任何主資源對象的重定向。

我想用一個數組作爲輸入到這個過程中,如:

var pageUrl = [ 
'http://www.google.com', 
'http://www.facebook.com' 
]; 

這裏的原

var sys = require('system'); 
var pageUrl = 'http://www.google.com'; 


console.log("Requested URL: " + pageUrl); 



var renderPage = function (url) { 
    var page = require('webpage').create(); 

    page.onNavigationRequested = function(url, type, willNavigate, main) { 

     if (main && url!=pageUrl) { 
      console.log("Redirected URL: " + url) 
     } 
    }; 



    page.open(url, function(status) { 
      if (status !== 'success') { 
       phantom.exit(1); 
      } else { 
       setTimeout(function() { 
        phantom.exit(0); 
       }, 0); 
      } 
     }); 
}; 


renderPage(pageUrl); 
+0

我不明白。您在上一個問題中找到了[回答](http://stackoverflow.com/a/32332822)。你爲什麼再問一次。我還提出了兩個解決方案,對您之前的問題發表評論。 –

+0

這不是一個騙局,這是一個單獨的問題 – user3731460

+0

您的意見是不建設性的。這段代碼沒有壞掉。我在問一個有效的問題。 – user3731460

回答

3
var urls = [ 
'http://www.google.com', 
'http://www.facebook.com' 
]; 


function process() { 
    if (urls.length == 0) { 
     phantom.exit(); 
    } else { 
     //remove the first item of an array 
     url = urls.shift(); 
     //open a page 
     page = require('webpage').create(); 

     //store the requested url in a separate variable 
     var currentUrl = url 


     page.open(url, onFinishedLoading) 

     page.onNavigationRequested = function(url, type, willNavigate, main) { 
      console.log('\n' + currentUrl + '\nredirecting to \n' + url); 
     } 

    } 
} 

function onFinishedLoading(status) { 

    console.log(status); 
    page.release(); 
    process(); 
} 

process(); 
+0

我可以在代碼中添加待辦事項? – PossessWithin