如何使用 phantomjs 将 html 源代码打印到控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12450868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print html source to console with phantomjs
提问by toom
I just downloaed and installed phantomjs on my machine. I copy and pasted the following script into a file called hello.js:
我刚刚在我的机器上下载并安装了 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);
I'd like to print the complete html source (in this case from the google page) to a file or to the console. How do I do this?
我想将完整的 html 源代码(在本例中来自谷歌页面)打印到文件或控制台。我该怎么做呢?
回答by Ariya Hidayat
Spent some time to read the documentation, it should be obvious afterwards.
花了一些时间阅读文档,之后应该很明显。
var page = require('webpage').create();
page.open('http://google.com', function () {
console.log(page.content);
phantom.exit();
});