我遵循本教程,并具有创建网站的图像。我想要的是输出网站而不是图像的文本。
config.ru
文件看起来像这样:
require 'sinatra/base'
require 'digest/md5'
class App < Sinatra::Base
get '/' do
return "to specifiy the rendered URL use "?url=<some url>"" unless params[:url]
digest = Digest::MD5.hexdigest(params[:url])
system(File.expand_path("~/app-root/data/phantomjs/bin/phantomjs"), File.expand_path("~/app-root/data/phantomjs/examples/rasterize.js"), params[:url], "public/#{digest}.png")
digest
end
end
run App
我找到了一个用于输出内容的示例,这是代码content.js
:
var webPage = require('webpage');
var page = webPage.create();
page.open('http://google.com', function () {
console.log(page.content);
phantom.exit();
});
所以有两个问题:
- 如何打印出
page.content
? - 如何将
url
查询字符串参数传递给content.js
文件?
要使用 console.log
,您需要编写一个特殊的侦听器 - 请参阅OnConsoLemessage。顺便说一句,为了查看错误,也有类似的东西。
这些文档提供了一个示例:
var webPage = require('webpage');
var page = webPage.create();
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
要将参数传递到幻影脚本,我搜索了现有的stackoverflow问题并找到了phantomjs-将参数传递给JS文件。看来这样做的方法是使用命令行参数。有关更多详细信息,请参见args
上的幻影文档。
看来您的system
调用还可以,但是在幻影脚本中,您需要使用args
来读取传递的参数。