使用PhantomJS和node.js保存并呈现网页 [英] Save and render a webpage with PhantomJS and node.js

查看:94
本文介绍了使用PhantomJS和node.js保存并呈现网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个请求网页的例子,等待JavaScript呈现(JavaScript修改DOM),然后抓取页面的HTML。



<这应该是一个简单的例子,PhantomJS有一个明显的用例。我无法找到一个体面的例子,文档似乎都是关于命令行的使用。 解决方案

'd猜你有2个选项


  1. 尝试找到phantomjs节点模块 - https://github.com/sgentle/phantomjs-node

  2. 在节点内运行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

  1. Try to find a phantomjs node module - https://github.com/sgentle/phantomjs-node
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆