使用PhantomJS和node.js保存并呈现网页 [英] Save and render a webpage with PhantomJS and node.js
问题描述
我正在寻找一个请求网页的例子,等待JavaScript呈现(JavaScript修改DOM),然后抓取页面的HTML。
<这应该是一个简单的例子,PhantomJS有一个明显的用例。我无法找到一个体面的例子,文档似乎都是关于命令行的使用。 解决方案
'd猜你有2个选项
- 尝试找到phantomjs节点模块 - https://github.com/sgentle/phantomjs-node
- 在节点内运行phantomjs作为子进程 - http://nodejs.org/api/child_process.html
编辑:
似乎phantomjs建议子进程作为与节点交互的一种方式,请参阅faq - http://code.google.com/p/phantomjs/wiki/FAQ
编辑:
示例用于获取页面的Phantomjs脚本HTML标记:
var page = require('webpage')。create();
page.open('http://www.google.com',function(status){
if(status!=='success'){
console.log('Unable访问网络');
} else {
var p = page.evaluate(function(){
return document.getElementsByTagName('html')[0] .innerHTML
});
console.log(p);
}
phantom.exit();
});
I'm looking for an example of requesting a webpage, waiting for the JavaScript to render (JavaScript modifies the DOM), and then grabbing the HTML of the page.
This should be a simple example with an obvious use-case for PhantomJS. I can't find a decent example, the documentation seems to be all about command line use.
From your comments, I'd guess you have 2 options
- Try to find a phantomjs node module - https://github.com/sgentle/phantomjs-node
- Run phantomjs as a child process inside node - http://nodejs.org/api/child_process.html
Edit:
It seems the child process is suggested by phantomjs as a way of interacting with node, see faq - http://code.google.com/p/phantomjs/wiki/FAQ
Edit:
Example Phantomjs script for getting the pages HTML markup:
var page = require('webpage').create();
page.open('http://www.google.com', function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var p = page.evaluate(function () {
return document.getElementsByTagName('html')[0].innerHTML
});
console.log(p);
}
phantom.exit();
});
这篇关于使用PhantomJS和node.js保存并呈现网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!