如何使用PhantomJS获取网站的HTML源代码 [英] How to get the HTML source of a website with PhantomJS

查看:106
本文介绍了如何使用PhantomJS获取网站的HTML源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个PhantomJS示例,它通过外部网页上的DOM ID获取一些元素:

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();
});

我想获取网页的整个HTML源代码...我该怎么做?

I want to get the entire HTML source of a webpage ... how do I do this?

推荐答案

您所要做的就是使用 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();
});

这篇关于如何使用PhantomJS获取网站的HTML源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆