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

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

问题描述

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

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.

这应该是一个带有明显 PhantomJS 用例的简单示例.我找不到一个像样的例子,文档似乎都是关于命令行使用的.

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.

推荐答案

根据你的评论,我猜你有 2 个选择

From your comments, I'd guess you have 2 options

  1. 尝试找一个phantomjs节点模块——https://github.com/amir20/phantomjs-node
  2. 在节点内将 phantomjs 作为子进程运行 - http://nodejs.org/api/child_process.html

phantomjs 似乎建议将子进程作为与节点交互的一种方式,请参阅常见问题解答 - http://code.google.com/p/phantomjs/wiki/FAQ

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

用于获取页面 HTML 标记的 Phantomjs 脚本示例:

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天全站免登陆