2016-11-23 146 views
1

我試圖用PhantomJS加載頁面並將其保存爲.png。但是,創建的png看起來並不像原來的那樣,並且缺少大部分正文。在線搜索中,大部分類似的問題都是因爲沒有足夠長的時間來加載頁面。但是,這還沒有解決我的問題。這是我正在運行:PhantomJS未加載動態內容

var page = require('webpage').create(); 
var websiteAddress = 'http://poe.ninja/standard/currency'; 
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'; 
//viewportSize being the actual size of the headless browser 
page.viewportSize = { width: 1920, height: 1080 }; 
//the clipRect is the portion of the page you are taking a screenshot of 
page.clipRect = { top: 0, left: 0, width: 1920, height: 1080 }; 

page.open(websiteAddress, function (status) { 
    setTimeout(function(){ 
     page.render('output.png'); 
     phantom.exit(); 
    }, 5000); // 5 sec should be enough 
}); 

我做錯了什麼或者這是一個在PhantomJS中的錯誤?

下面是它應該是什麼樣子和圖像它實際上是這樣的:

預計:Expected 實際:Actual

+1

添加[page.onError](http://phantomjs.org/api/webpage/handler /on-error.html)回調來檢查是否有任何錯誤。 – Vaviloff

回答

0

使用page.onResourceRequested = function(request) {}page.onResourceReceived = function(response) {}回調保持的開始理貨並完成資源下載。一旦您的onResourceReceived回撥檢測到所有下載都已完成,請致電page.render()。這比5秒超時更可靠。

雖然我建議等待0.5秒前等待onResourceReceived調用page.render(),因爲最後一個待處理資源可能會完成,但會觸發其他下載。

1

目標網站是一個React應用程序,它可能使用了當前PhantomJS中不可用的一些更新的ES6 JavaScript語法(因爲它使用較舊的QTWebkit呈現引擎)。

的解決方案是使用填充工具庫,以彌補那些缺失的方法,如在這個答案做:https://stackoverflow.com/a/38471938/2715393

+0

這樣做!謝謝! – Retik