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

javascript - How to get the HTML source of a website with PhantomJS - Stack Overflow

programmeradmin0浏览0评论

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('', 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?

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?

Share Improve this question edited Nov 24, 2013 at 12:03 robertklep 203k37 gold badges413 silver badges402 bronze badges asked Nov 24, 2013 at 11:26 MOBMOB 8532 gold badges13 silver badges28 bronze badges 1
  • If you want the HTML source, then use something like the http module rather then running the page through a browser (which will execute JS and mangle the DOM with it). – Quentin Commented Nov 24, 2013 at 11:34
Add a comment  | 

1 Answer 1

Reset to default 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();
});
发布评论

评论列表(0)

  1. 暂无评论