Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

phantomjs - Installing/Using Phantom.js with Meteor

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.

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

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"

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

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

Any clarification would be greatly appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

[EDIT] now meteor is supporting npm packages out of the box: https://guide.meteor.com/using-npm-packages.html#installing-npm


Here is the procedure for Meteor > 1.0.0

Add the npm package

meteor add meteorhacks:npm

Run meteor to let the npm package to pre-initialise

meteor

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");

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
    });
}

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();
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...