How to get the HTML source of a website using PhantomJS

The following is an example of PhantomJS that gets some DOM id element from an external web page:

var page = require('webpage').create(); console.log('The default user agent is ' + page.settings.userAgent); page.settings.userAgent = 'SpecialAgent'; page.open('http://www.httpuseragent.org', function(status) { if (status !== 'success') { console.log('Unable to access network'); } else { var ua = page.evaluate(function() { return document.getElementById('myagent').textContent; }); console.log(ua); } phantom.exit(); }); 

I want to get the whole HTML source of a web page ... how to do this?

+8
javascript phantomjs
source share
1 answer

All you have to do is use page.content

 var page = require('webpage').create(); page.onError = function(msg, trace) { //prevent js errors from showing in page.content return; }; page.open('http://www.httpuseragent.org', function () { console.log(page.content); //page source phantom.exit(); }); 
+10
source share

All Articles