最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - phantomjs doesn't render a page - Stack Overflow

programmeradmin1浏览0评论

I was doing some research in many websites today and, to avoid looking at them manually, I prepared phantomjs to render them using the solution proposed here. Nothing special. Looping through a website array and rendering all the resulting pages.

What's strange is that there are some websites that are not being properly rendered. Among others, I have this one: /

To simplify, I created another script that only runs this page:

var page = require('webpage').create();

page.viewportSize = { width: 1920, height: 960 };
page.clipRect = { top: 0, left: 0, width: 1920, height: 960 };

page.open('/', function(status) {
  page.render("screenshot.png");
  phantom.exit();
});

It ends in no screenshot. Tested with any other one, and perfectly working. Did I overlook something?

I was doing some research in many websites today and, to avoid looking at them manually, I prepared phantomjs to render them using the solution proposed here. Nothing special. Looping through a website array and rendering all the resulting pages.

What's strange is that there are some websites that are not being properly rendered. Among others, I have this one: http://www.telegraaf.nl/

To simplify, I created another script that only runs this page:

var page = require('webpage').create();

page.viewportSize = { width: 1920, height: 960 };
page.clipRect = { top: 0, left: 0, width: 1920, height: 960 };

page.open('http://www.telegraaf.nl/', function(status) {
  page.render("screenshot.png");
  phantom.exit();
});

It ends in no screenshot. Tested with any other one, and perfectly working. Did I overlook something?

Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked Nov 12, 2015 at 17:52 Sergi JuanolaSergi Juanola 6,6478 gold badges59 silver badges94 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

It doesn't render a screenshot, because the page has no <body> initially and therefore nothing to render. Everything, including the body, is loaded through JavaScript after PhantomJS' onLoadFinished event fires.

You need to wait a little for a full page load. A simple 5 second wait was sufficient for me:

page.open('http://www.telegraaf.nl/', function(status) {
    setTimeout(function(){
        page.render("screenshot.png");
        phantom.exit();
    }, 5000);
});

You can of course wait in a more fancy way in order to make it more robust and not to wait too long: phantomjs not waiting for “full” page load


You may need to run PhantomJS with --ignore-ssl-errors=true (and maybe --ssl-protocol=any if PhantomJS <1.9.8).

发布评论

评论列表(0)

  1. 暂无评论