11

Below is an example of PhantomJS that gets some element by DOM id from an external webpage:

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 entire HTML source of a webpage ... how do I do this?

robertklep
  • 198,204
  • 35
  • 394
  • 381
MOB
  • 853
  • 2
  • 13
  • 28
  • If you want the HTML source, then use something like [the http module](http://nodejs.org/docs/v0.5.2/api/http.html#http.request) rather then running the page through a browser (which will execute JS and mangle the DOM with it). – Quentin Nov 24 '13 at 11:34

1 Answers1

14

All you have to do is to 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();
});
user3338098
  • 907
  • 1
  • 17
  • 38
Hessam
  • 1,377
  • 1
  • 23
  • 45