【问题标题】:SpookyJS has no Start Method while using it in MeteorSpookyJS 在 Meteor 中使用时没有 Start 方法
【发布时间】:2015-04-26 22:08:06
【问题描述】:

我有一个奇怪的错误,在过去的几个小时里找不到它的原因...

我有一个流星应用程序,它会抓取一些网页以获取信息,只要我对静态页面使用 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 是否正确安装在我的应用程序中,但这对于第一次使用它们的人来说并不像听起来那么容易......

【问题讨论】:

  • 你安装了 CasperJS 并且 Spooky 能够找到它吗?
  • 是的,我查过了。问题是,我必须创建一个新的幽灵,如下所示:'var Spooky = Meteor.npmRequire('spooky'); var spooky = new Spooky({' 现在它就像一个魅力......主要......
  • 如果你解决了,你可以自己提供答案。

标签: node.js meteor npm phantomjs spookyjs


【解决方案1】:

与 spooky 一样,您必须先创建一个新的 Spooky 对象,然后才能启动和运行它...

 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(/ \- .*/, ''));
          }
      });
  })
}

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多