12

I just downloaed and installed phantomjs on my machine. I copy and pasted the following script into a file called hello.js:

var page = require('webpage').create();
var url = 'https://www.google.com'

page.onLoadStarted = function () {
    console.log('Start loading...');
};

page.onLoadFinished = function (status) {
    console.log('Loading finished.');
phantom.exit();
};

page.open(url);

I'd like to print the complete html source (in this case from the google page) to a file or to the console. How do I do this?

toom
  • 12,864
  • 27
  • 89
  • 128

1 Answers1

53

Spent some time to read the documentation, it should be obvious afterwards.

var page = require('webpage').create();
page.open('http://google.com', function () {
    console.log(page.content);
    phantom.exit();
});
Ariya Hidayat
  • 12,523
  • 3
  • 46
  • 39
  • 5
    this would give the html from the dom (which has been manipulated by javascript and has undergone some level of syntax correction) as opposed to the raw unprocessed html served by the server. Is there a way one can get that? maybe from onResourceReceived? – Surender Thakran Jan 04 '15 at 16:20
  • Hey AriyaHidayat! is it also possible to receive the css with phantomjs? I didn't see anything about that yet – BoJack Horseman Jan 15 '16 at 07:11
  • how to authenticate user & pass over https @toom – lazydeveloper Jan 22 '18 at 17:48
  • Hi, @Ariya Hidayat : not working in my case. Tried with my website and another : http://www.nolme.com https://www.centrale-canine.fr/lofselect/chien-ws/1/ page.content is empty or just header. Did I miss something ? – Nolmë Informatique May 21 '18 at 21:35