簡體   English   中英

如何使用PhantomJS獲取網站的HTML源代碼

[英]How to get the HTML source of a website with PhantomJS

下面是一個PhantomJS示例,它通過外部網頁上的DOM id獲取一些元素:

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源代碼...我該怎么做?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM