2015-09-01 16 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