SpookyJS 在 Meteor 中使用时没有启动方法

SpookyJS has no Start Method while using it in Meteor

我有一个奇怪的错误,最近几个小时都找不到原因...

我有一个 meteor 应用程序,它会抓取一些网页以获取信息,只要我对静态页面使用 reuqest 和 cheerio 一切正常,但现在我有一个动态站点,我想使用 phantomjs、casperjs 和 spookyjs对于这个,但在这里我遇到了一些错误...... 我的代码如下,一开始我导入了npm模块:

    if (Meteor.isServer) {
    var cheerio = Meteor.npmRequire('cheerio');
    var request = Meteor.npmRequire('request');
    var phantomJS = Meteor.npmRequire('phantomjs');
    var spooky = Meteor.npmRequire('spooky');

稍后我想使用 spooky 来抓取一些网页:

 spooky.start("https://www.coursera.org/");

  spooky.then( function () {
    this.fill("form", {email: user, password: pass}, true);
  });`

但是我一调用该方法就收到以下错误消息:

    20150224-21:16:39.100(-5)? Exception while invoking method 'getLecturesCoursera' TypeError: Object function Spooky(options, callback) {
    ....
    I20150224-21:16:39.281(-5)? } has no method 'start'
    I20150224-21:16:39.281(-5)?     at [object         Object].Meteor.methods.getLecturesCoursera (app/moocis.js:72:14)

我做的事情完全错了,我不知道为什么它不起作用... 我试图验证 spookyjs 和 phantomjs 是否正确安装在我的应用程序中,但这对于第一次使用它们的人来说并不像听起来那么容易...

与通常使用幽灵一样,您必须先创建一个新的幽灵对象,然后才能开始 运行 它...

 if (Meteor.isServer) {
  Meteor.startup( function () {
    var Spooky = Meteor.npmRequire('spooky');

    var spooky = new Spooky({
          child: {
              transport: 'http'
          },
          casper: {
              logLevel: 'debug',
              verbose: true,
              ignoreSslErrors: true,
              sslProtocol: 'tlsv1'
          }
      }, function (err) {
          if (err) {
              e = new Error('Failed to initialize SpookyJS');
              e.details = err;
              throw e;
          }

          spooky.start(
              'https://www.google.com');
          spooky.then(function () {
              this.emit('hello', 'Hello, from ' + this.evaluate(function () {
                  return document.title;
              }));
          });
          spooky.run();
      });

      spooky.on('error', function (e, stack) {
        console.error(e);

        if (stack) {
          console.log(stack);
        }
      });

      spooky.on('hello', function (greeting) {
          console.log(greeting);
      });

      spooky.on('log', function (log) {
          if (log.space === 'remote') {
              console.log(log.message.replace(/ \- .*/, ''));
          }
      });
  })
}