2016-11-23 65 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