2015-09-08 96 views
2

以下腳本包含links數組中的一些URL。函數gatherLinks()用於從links數組中的URL的sitemap.xml中收集更多URL。一旦links數組有足夠的URL(由變量limit決定),函數request()被調用,每個URL在links數組中向服務器發送請求,獲取響應並使用page.render()函數保存圖像。PhantomJS 2.0.0不會等待頁面加載

問題是,當我使用PhantomJS 2.0.0運行它時,許多圖像缺乏很多內容,即PhantomJS可能不會等待所有內容加載。但是當我使用PhantomJS 1.9.8時,所有內容都可以正常加載。可能是什麼原因?

var webpage = require('webpage'); 
var system = require('system'); 
var fs = require('fs'); 
var links = []; 

links = [ 
    "http://somesite.com", 
    "http://someothersite.com", 
     . 
     . 
     . 
]; 

var index = 0, fail = 0, limit = 20; 
finalTime = Date.now(); 

var gatherLinks = function(link){ 
    var page = webpage.create(); 
    link = link + "/sitemap.xml"; 
    console.log("Fetching links from " + link); 

    page.open(link, function(status){ 
    if(status != "success"){ 
     console.log("Sitemap Request FAILED, status: " + status); 
     fail++; 
     return; 
    } 

    var content = page.content; 
    parser = new DOMParser(); 
    xmlDoc = parser.parseFromString(content, 'text/xml'); 
    var loc = xmlDoc.getElementsByTagName('loc'); 

    for(var i = 0; i < loc.length; i++){ 
     if(links.length < limit){ 
     links[links.length] = loc[i].textContent; 
     } else{ 
     console.log(links.length + " Links prepared. Starting requests.\n"); 
     index = 0; 
     page.close(); 
     request(); 
     return; 
     } 
    } 

    if(index >= links.length){ 
     index = 0; 
     console.log(links.length + " Links prepared\n\n"); 
     page.close(); 
     request(); 
     return; 
    } 

    page.close(); 
    gatherLinks(links[++index]); 
    }); 
}; 

var request = function(){ 
    t = Date.now(); 
    var page = webpage.create(); 
    page.open(links[index], function(status) { 
    console.log('Loading link #' + (index + 1) + ': ' + links[index]); 
    console.log("Time taken: " + (Date.now() - t) + " msecs"); 

    if(status != "success"){ 
     console.log("Request FAILED, status: " + status); 
     fail++; 
    } 

    page.render("img_200_" + index + ".jpeg", {format: 'jpeg', quality: '100'}); 
    if(index >= links.length-1){ 
     console.log("\n\nAll links done, final time taken: " + (Date.now() - finalTime) + " msecs"); 
     console.log("Requests sent: " + links.length + ", Failures: " + fail); 
     console.log("Success ratio: " + ((links.length - fail)/links.length)*100 + "%"); 
     page.close(); 
     phantom.exit(); 
    } 

    index++; 
    page.close(); 
    request(); 
    }); 
} 

gatherLinks(links[0]); 
+0

我同樣有很多麻煩讓PhantomJS和CasperJS等待整頁加載。我試圖遵循這個建議:http://stackoverflow.com/a/27472788/470749 – Ryan

回答

0

PhantomJS沒有定義何時在頁面加載過程中調用page.open回調函數。所以,沒有任何錯誤地聲稱。

這可能是因爲您可以添加靜態等待金額setTimeout()這應該有助於動態網站。還有一些方法可以通過計算以page.onResourceRequested發送多少個請求以及多少個請求以page.onResourceReceived/page.onResourceTimeout/page.onResourceError完成,來查看是否有未決請求。

如果它實際上是一個PhantomJS錯誤,那麼除了嘗試一些命令行開關外,沒有太多可以做到。