PhantomJS - content 属性

此属性包含网页的内容。

语法

其语法如下 −

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

为了展示一个示例,让我们打开一个页面和控制台,看看我们在 page.content 中得到了什么。

稍后将详细讨论 open website 方法。现在,我们将使用它来解释它的属性。

示例

以下示例展示了如何使用 content 属性。

var wpage = require('webpage').create(),url  = 'http://localhost/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 函数的工作原理与浏览器的 view source 函数相同。

phantomjs_webpage_module_properties.html