Phantomjs-content-property

提供:Dev Guides
移動先:案内検索

PhantomJS-コンテンツプロパティ

このプロパティには、Webページのコンテンツが含まれます。

構文

その構文は次のとおりです-

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

例を示すために、ページとコンソールを開き、 page.content で何が得られるかを見てみましょう。

*open webpage method* の詳細については、後で説明します。 今のところ、それを使用してプロパティを説明します。

次の例は、 content プロパティの使用方法を示しています。

var wpage = require('webpage').create(),url  = 'http://localhost/tasks/al';
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();
   }
});

上記のプログラムは、次の output を生成します。

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 関数と同じように機能します。