【问题标题】:Cron and nightmare jsCron 和噩梦 js
【发布时间】:2016-11-04 12:40:38
【问题描述】:

我正在尝试在本地运行带有噩梦 js 的 cron。 不幸的是我有这个错误。

Unhandled rejection (<{"message":"navigation error","code":-...>, no stack trace)

相关问题:Nightmare JS not working

我想知道这是否与噩梦需要图形界面这一事实有关?

感谢您的帮助,

编辑

在我的 cron 中,我有一个 Promise 函数,它由 cron 和 Promise 组成。

var job = new CronJob('* */10 * * * *', function() {
    crawl()
  }, function () {
    console.log("crawl ended")
  },
  true
);


job.start();

这是噩梦的样子:

var Nightmare = require('nightmare');
var nightmare = Nightmare({
  typeInterval: 300,
  show: true
});

nightmare
  .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
  .type('[name=email]', '')
  .wait(1000)
  .type('[name=email]', 'myemail')
  .wait(1000)
  .type('[name=password]', '')
  .wait(1000)
  .type('[name=password]', 'mypassword')
  .click('[type=submit]')
  .wait(25000)
  .wait(25000)
  .evaluate(function (page, done) {

    document.documentElement
    done()
  })
  .end()
  .then(function (result) {
    // fs.writeFileSync('testOutput.json', JSON.stringify(result));
    console.log(JSON.stringify(result))
  })
  .catch(function (error) {
    console.error('failed:', error);
  });

当我在没有 cron 的情况下运行 crawl 函数时,它工作得很好。

【问题讨论】:

    标签: javascript cron nightmare


    【解决方案1】:

    好吧,我不确定我是否正确,因为我对此没有太多经验,而且您还没有指定您在 cron 中定义的内容。但是从快速搜索中,我做出了你所猜测的事情是正确的。当您使用 cron 时,您的调用是通过命令行进行的。现在 Nightmare 建立在 Electron 之上,而 Electron 又依赖于 Chromium。现在从我了解到的here 中,Electron 可能有一个错误,每次页面在真正的 Chromium 浏览器上立即加载时都会导致超时。因此,从我目前收集到的信息来看,您的应用程序需要 Electron 与 Chromium 通信才能正常工作,而在您的情况下,它似乎没有这样做。很抱歉含糊不清,可能是错误的,但我能想到的最好的信息这么少。

    【讨论】:

    • 感谢您尝试帮助解决如此模糊的问题。 +1
    • 感谢您的帮助。我会尝试使用 PhantomJS。
    • @QuentinDel nightmare 非常出色
    • @naomik 感谢您的提示我在这里问了一个关于噩梦的更精确的问题:stackoverflow.com/questions/40379389/cron-and-nightmarejs
    【解决方案2】:

    我的问题在于 cron 的设置。 我宁愿使用

    var job = new CronJob('* 10 * * * *', function() {
        crawl()
      }, function () {
        console.log("crawl ended")
      },
      true
    );
    

    另外,我不得不将噩梦设置重新定义到我的功能中。

    var get_data = function(){
      var Nightmare = require('nightmare');
      var nightmare = Nightmare({
        typeInterval: 300,
        show: true
      });
      nightmare
      .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
      .type('[name=email]', '')
      .wait(1000)
      .type('[name=email]', 'myemail')
      .wait(1000)
      .type('[name=password]', '')
      .wait(1000)
      .type('[name=password]', 'mypassword')
      .click('[type=submit]')
      .wait(25000)
      .wait(25000)
      .evaluate(function (page, done) {
    
        document.documentElement
        done()
      })
      .end()
      .then(function (result) {
        // fs.writeFileSync('testOutput.json', JSON.stringify(result));
        console.log(JSON.stringify(result))
      })
      .catch(function (error) {
        console.error('failed:', error);
      });
    }
    

    代替

    var Nightmare = require('nightmare');
    var nightmare = Nightmare({
      typeInterval: 300,
      show: true
    });
    
    var get_data = function(){
      nightmare
      .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
      .type('[name=email]', '')
      .wait(1000)
      .type('[name=email]', 'myemail')
      .wait(1000)
      .type('[name=password]', '')
      .wait(1000)
      .type('[name=password]', 'mypassword')
      .click('[type=submit]')
      .wait(25000)
      .wait(25000)
      .evaluate(function (page, done) {
    
        document.documentElement
        done()
      })
      .end()
      .then(function (result) {
        // fs.writeFileSync('testOutput.json', JSON.stringify(result));
        console.log(JSON.stringify(result))
      })
      .catch(function (error) {
        console.error('failed:', error);
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2020-03-14
      • 2010-11-13
      相关资源
      最近更新 更多