使用没有ES6语法和产量的Nightmare.js

时间:2022-04-07 23:28:50

I built a simple node script using nightmare.js to scrape websites

我使用nightmare.js构建了一个简单的节点脚本来抓取网站

var Nightmare = require('nightmare');
var vo = require('vo');

vo(run)(function(err, result) {
    if (err) throw err;
});

function *run() {
    var x = Date.now();
    var nightmare = Nightmare();
    var html = yield nightmare
    .goto('http://google.com')
    .evaluate(function() {
        return document.getElementsByTagName('html')[0].innerHTML;
    });

    console.log("done in " + (Date.now()-x) + "ms");
    console.log("result", html);

    yield nightmare.end();
}

I want to run this in an environment using an older version of node, which does not support ES6 features. There are no examples on the github page on how to do this without the "yield" keyword.

我想在使用旧版本节点的环境中运行它,该节点不支持ES6功能。如果没有“yield”关键字,github页面上没有关于如何执行此操作的示例。

I did find an example of usage without the ES6 syntax here : Webscraping with nightmare

我确实在这里找到了一个没有ES6语法的使用示例:Webscraping with nightmare

I wrote it like this :

我这样写的:

var night = new Nightmare()
.goto('http://www.google.com')
.evaluate(function () {
  return document.getElementsByTagName('html')[0].innerHTML;
},function (html) {
   console.log("result", html);
  }
)
.run(function (err, nightmare) {
  if (err) return console.log(err);
  console.log('Done!');
});

It does not crash, but the result logging function is never called.

它不会崩溃,但永远不会调用结果日志记录功能。

With the yield syntax, getting the returned value from "evaluate" is pretty straightforward, but without it, I did not find any way to do it.

使用yield语法,从“evaluate”获取返回值非常简单,但没有它,我没有找到任何方法来做到这一点。

UPDATE Wrote this thanks to the accepted answer and its comments. It uses 'Q' and works in node versions previous to 0.12:

更新写了感谢接受的答案和评论。它使用'Q'并在0.12之前的节点版本中工作:

var Nightmare = require('nightmare');

var Promise = require('q').Promise;

var x = Date.now();
var nightmare = Nightmare();
Promise.resolve(nightmare
  .goto('http://google.com')
  .evaluate(function() {
      return document.getElementsByTagName('html')[0].innerHTML;
})).then(function(html) {
    console.log("done in " + (Date.now()-x) + "ms");
    console.log("result", html);
    return nightmare.end();
}).then(function(result) {

}, function(err) {
   console.error(err); // notice that `throw`ing in here doesn't work
});

1 个解决方案

#1


12  

The docs are horrible, but it seems that Nightmare is based on thenables. I didn't find much information on the callback interface either, but that would lead to an indentation pyramid anyway.

文档太可怕了,但似乎Nightmare是基于theables。我也没有在回调接口上找到太多信息,但这无论如何都会导致缩进金字塔。

So your best bet is to use promises, just choose any library that roughly follows the ES6 standard (they all are usable in non-ES6 environments as well).

所以你最好的选择是使用promises,只需选择任何大致符合ES6标准的库(它们都可以在非ES6环境中使用)。

You can easily transform your linear generator code into a promise chain, just replace every yield by a then call:

您可以轻松地将线性生成器代码转换为promise链,只需通过调用替换每个yield:

var Nightmare = require('nightmare');
var Promise = require('…');

var x = Date.now();
var nightmare = Nightmare();
Promise.resolve(nightmare
  .goto('http://google.com')
  .evaluate(function() {
      return document.getElementsByTagName('html')[0].innerHTML;
})).then(function(html) {
    console.log("done in " + (Date.now()-x) + "ms");
    console.log("result", html);
    return nightmare.end();
}).then(function(result) {
    …
}, function(err) {
   console.error(err); // notice that `throw`ing in here doesn't work
});

#1


12  

The docs are horrible, but it seems that Nightmare is based on thenables. I didn't find much information on the callback interface either, but that would lead to an indentation pyramid anyway.

文档太可怕了,但似乎Nightmare是基于theables。我也没有在回调接口上找到太多信息,但这无论如何都会导致缩进金字塔。

So your best bet is to use promises, just choose any library that roughly follows the ES6 standard (they all are usable in non-ES6 environments as well).

所以你最好的选择是使用promises,只需选择任何大致符合ES6标准的库(它们都可以在非ES6环境中使用)。

You can easily transform your linear generator code into a promise chain, just replace every yield by a then call:

您可以轻松地将线性生成器代码转换为promise链,只需通过调用替换每个yield:

var Nightmare = require('nightmare');
var Promise = require('…');

var x = Date.now();
var nightmare = Nightmare();
Promise.resolve(nightmare
  .goto('http://google.com')
  .evaluate(function() {
      return document.getElementsByTagName('html')[0].innerHTML;
})).then(function(html) {
    console.log("done in " + (Date.now()-x) + "ms");
    console.log("result", html);
    return nightmare.end();
}).then(function(result) {
    …
}, function(err) {
   console.error(err); // notice that `throw`ing in here doesn't work
});