本文介绍了使用 Meteor 安装/使用 Phantom.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力将 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 > 中看到这一点本地 >构建 >程序 >服务器 >npm 目录下有一个 phantomjs 目录.

I can see that in my .meteor > local > build > programs > server > npm directory there is a phantomjs directory.

我的问题是,我如何实际使用 Phantom?我试图从服务器端抓取东西.我尝试了以下事情(使用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")

(I've also tried using capital "P's")

所有以这种方式尝试的结果:错误:找不到模块'phantomjs'

All attempts in this way yield: Error: Cannot find module 'phantomjs'

任何澄清将不胜感激!

推荐答案

现在meteor 支持开箱即用的npm 包:https://guide.meteor.com/using-npm-packages.html#installing-npm

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

运行meteor让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"
}

要在您的服务器端代码中使用 phantom:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:24
查看更多