【问题标题】:How to print html source to console with phantomjs如何使用 phantomjs 将 html 源代码打印到控制台
【发布时间】:2012-09-16 21:28:12
【问题描述】:

我刚刚在我的机器上下载并安装了 phantomjs。我将以下脚本复制并粘贴到名为 hello.js 的文件中:

var page = require('webpage').create();
var url = 'https://www.google.com'

page.onLoadStarted = function () {
    console.log('Start loading...');
};

page.onLoadFinished = function (status) {
    console.log('Loading finished.');
phantom.exit();
};

page.open(url);

我想将完整的 html 源代码(在本例中来自 google 页面)打印到文件或控制台。我该怎么做?

【问题讨论】:

    标签: html web-crawler phantomjs


    【解决方案1】:

    花了一些时间阅读文档,之后应该很明显。

    var page = require('webpage').create();
    page.open('http://google.com', function () {
        console.log(page.content);
        phantom.exit();
    });
    

    【讨论】:

    • 这将提供来自 dom 的 html(它已被 javascript 操作并经过某种程度的语法更正),而不是服务器提供的原始未处理的 html。有没有办法得到它?也许来自 onResourceReceived?
    • 嘿AriyaHidayat!是否也可以使用 phantomjs 接收 css?我还没有看到任何相关内容
    • 如何验证用户并通过 https @toom
    • 嗨,@Ariya Hidayat:在我的情况下不起作用。尝试使用我的网站和另一个:nolme.comcentrale-canine.fr/lofselect/chien-ws/1 page.content 为空或只是标题。我错过了什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    相关资源
    最近更新 更多