Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Save and render a webpage with PhantomJS and node.js

Save and render a webpage with PhantomJS and node.js

Problem

I'm looking for an example of requesting a Webpage, waiting for the JavaScript to Render (JavaScript modifies the DOM), and then grabbing the HTML of the page.

This should be a simple example with an obvious use-case for Phantomjs. I can't find a decent example, the documentation seems to be all about command line use.

Problem courtesy of: Harry

Solution

From your comments, I'd guess you have 2 options

  1. Try to find a phantomjs node module - https://github.com/sgentle/phantomjs-node
  2. Run phantomjs as a child process inside node - http://nodejs.org/api/child_process.html

Edit:

It seems the child process is suggested by phantomjs as a way of interacting with node, see faq - http://code.google.com/p/phantomjs/wiki/FAQ

Edit:

Example Phantomjs script for getting the pages HTML markup:

var page = require('webpage').create();  
page.open('http://www.google.com', function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        var p = page.evaluate(function () {
            return document.getElementsByTagName('html')[0].innerHTML
        });
        console.log(p);
    }
    phantom.exit();
});
Solution courtesy of: Declan Cook

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Save and render a webpage with PhantomJS and node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×