PhantomJS - 內容屬性



此屬性包含網頁的內容。

語法

其語法如下 −

var page = require('webpage').create();
page.content;

下面我們開啟一個頁面和控制檯來演示 page.content 中顯示的內容。

關於 開啟網頁方法 的詳細資訊,後面會討論。現在,我們使用該方法來說明這種屬性。

示例

以下示例演示如何使用 content 屬性。

var wpage = require('webpage').create(),url  = 'https:///tasks/a.html'; 
wpage.open(url, function(status) { 
   if (status) { 
      console.log(status); 
      var content = wpage.content; 
      console.log('Content: ' + content); 
      phantom.exit(); 
   } else { 
      console.log("could not open the file");  
      phantom.exit(); 
   }   
});

以上程式生成以下 輸出

Success 
Content: <html>
   <head></head> 

   <body> 
      <script type = "text/javascript"> 
         console.log('welcome to cookie example'); 
         document.cookie = "username = Roy; expires = Thu, 22 Dec 2017 12:00:00 UTC"; 
      </script> 
      
      <h1>This is a test page</h1> 
      <h1>This is a test page</h1> 
      <h1>This is a test page</h1> 
      <h1>This is a test page</h1> 
      <h1>This is a test page</h1> 
      <h1>This is a test page</h1> 
      <h1>This is a test page</h1> 
      <h1>This is a test page</h1> 
      <h1>This is a test page</h1>  
   </body>
</html> 

這裡,我們將使用本地頁面來獲取內容以及上方顯示的頁面輸出。page.content 函式的工作方式恰如瀏覽器的 檢視原始碼 功能。

phantomjs_webpage_module_properties.htm
廣告
© . All rights reserved.