在Meteor上安装/使用Phantom.js [英] Installing/Using Phantom.js with Meteor
问题描述
我目前正在努力将Phantom.js与我的Meteor应用程序结合使用.我已经将其安装在本地计算机(Ubuntu 14.04)上,将其添加到我的路径中(可以从终端运行它),还运行并安装了Phantomjs的智能包装器:mrt add phantomjs
.
I'm currently struggling with using Phantom.js with a Meteor app of mine. I have it installed on my local machine (Ubuntu 14.04), it's added to my path (I can run it from my terminal), I also ran and installed the smart wrapper for Phantomjs: mrt add phantomjs
.
我可以看到在我的.meteor > local > build > programs > server > npm
目录中有一个phantomjs
目录.
I can see that in my .meteor > local > build > programs > server > npm
directory there is a phantomjs
directory.
我的问题是,我该如何实际使用幻影?我正在尝试从服务器端抓取信息.我已经尝试了以下操作(使用coffeescript):
phantom = Npm.require "phantomjs"
phantom = Npm.require "phantom"
phantom = Meteor.require "phantomjs"
phantom = Meteor.require "phantom"
My question is, how do I actually use Phantom? I'm attempting to scrape from the server side of things. I've tried the following things (using coffeescript):
phantom = Npm.require "phantomjs"
phantom = Npm.require "phantom"
phantom = Meteor.require "phantomjs"
phantom = Meteor.require "phantom"
(我也尝试使用大写的"P's")
(I've also tried using capital "P's")
以这种方式进行的所有尝试都会产生:Error: Cannot find module 'phantomjs'
All attempts in this way yield: Error: Cannot find module 'phantomjs'
任何澄清将不胜感激!
推荐答案
now meteor is supporting npm packages out of the box: https://guide.meteor.com/using-npm-packages.html#installing-npm
这是Meteor> 1.0.0的过程
Here is the procedure for Meteor > 1.0.0
添加npm软件包
meteor add meteorhacks:npm
运行流星,让npm包预先初始化
Run meteor to let the npm package to pre-initialise
meteor
已经在根目录下创建了一个packages.json文件.修改为:
A file packages.json has been created at the root. Edit it to:
{
"phantomjs": "1.9.13"
}
要在服务器端代码中使用幻像:
To use phantom into your server side code:
var phantomJS = Meteor.npmRequire("phantomjs");
奖金:使用示例(感谢Ben Green),将其放在代码中的任意位置:
Bonus: an example of usage (thanks Ben Green), put anywhere in your code:
if (Meteor.isServer) {
Meteor.startup(function () {
var phantomjs = Meteor.npmRequire('phantomjs');
var spawn = Meteor.npmRequire('child_process').spawn;
Meteor.methods({
runTest: function (options) {
command = spawn(phantomjs.path, ['assets/app/phantomDriver.js']);
command.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
command.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
command.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
}
});
Meteor.call("runTest");// run the test as soon as meteor server starts
});
}
创建phantomjs脚本文件./private/phantomDriver.js
并将其编辑为
Create the phantomjs script file ./private/phantomDriver.js
and edit it to
var page = require('webpage').create();
page.open('http://github.com/', function (){
console.log('Page Loaded');
page.render('github.png');
phantom.exit();
});
这篇关于在Meteor上安装/使用Phantom.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!