首先,我在开发或测试环境中不寻求任何帮助。我也是phantomjs的新手,我想要的只是phantomjs在Linux终端上的命令行操作。

我有一个html页面,其主体由一些javascript代码呈现。我需要的是我想使用phantomjs下载呈现的html内容。

我不知道使用phantomjs。我在Shell脚本方面有一些经验。所以我试图用curl做到这一点。但是由于curl不足以呈现javascript,因此我只能获取默认源代码的html。渲染的内容未下载。我听说 ruby 机械化可以完成这项工作。但是我对 ruby 一无所知。因此,在进一步调查中,我找到了命令行工具phantomjs。如何使用phantomjs做到这一点?

请随时询问我需要提供什么所有其他信息。

最佳答案

不幸的是,仅使用PhantomJS命令行是不可能的。您必须使用Javascript文件才能通过PhantomJS完成任何操作。

这是您可以使用的脚本的非常简单的版本

主要从https://stackoverflow.com/a/12469284/4499924复制的代码

printSource.js

var system = require('system');
var page   = require('webpage').create();
// system.args[0] is the filename, so system.args[1] is the first real argument
var url    = system.args[1];
// render the page, and run the callback function
page.open(url, function () {
  // page.content is the source
  console.log(page.content);
  // need to call phantom.exit() to prevent from hanging
  phantom.exit();
});

将页面源打印为标准输出。
phantomjs printSource.js http://todomvc.com/examples/emberjs/
要将页面源保存在文件
phantomjs printSource.js http://todomvc.com/examples/emberjs/ > ember.html

关于javascript - 使用phantomjs获取JavaScript呈现的html源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28209509/

10-16 01:18