PhantomJS content 属性读取页面内容实例


发布日期 : 2024-10-25 13:11:14 UTC

访问量: 10 次浏览

PhantomJS content 属性

content 属性包含网页的内容。

语法

content 语法如下所示:

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

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

打开网页方法的详细说明将在后面进行讨论。现在,我们将用它来解释其中的属性。

示例

下面的示例展示了如何使用 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 函数的工作方式就像浏览器的查看源代码功能一样。