Cron and nightmarejs

我只是一个虾纸丫 提交于 2019-12-08 12:58:27

Couple of things that jump out right away.

.evaluate(function (page, done) {

      return document.body.innerText
      done()
    })

This won't do what you expect it to do, and will likely never return and cause a timeout error. You're not passing in an argument for page, which means done will be undefined. Change the above to:

.evaluate(function (done) {

      return document.body.innerText
      done()
    })

Second, this:

.then(function(data){
      return fs.writeFile("./data.txt", data, function(err) {
        if(err) {
          console.log(err)
          reject(err)
        }
        resolve(data)
      });
    })

... redefines data. I don't think you're putting out the data variable set in the previous then, this should always output undefined, I'd think. Be careful with your closures.

Third, and perhaps most importantly:

.evaluate(function (page, done) {

      return document.body.innerText
      done()
    })
    .end() // <== this might be a problem
    .then(function (result) {
      data = result
    })

Since nightmare is only defined once, you're ending the only instance you have. It will not be recreated, and will not work properly if you try to execute actions on the ended instance in the second iteration of your loop. Either take the .end() out and move it to the end of your scripts, or create a new Nightmare instance for every iteration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!