使用没有ES6语法和收益的Nightmare.js [英] Use Nightmare.js without ES6 syntax and yield

查看:258
本文介绍了使用没有ES6语法和收益的Nightmare.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用nightmare.js构建了一个简单的节点脚本,以便刮擦网站。

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

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();
}

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

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语法的使用示例: Webscraping with噩梦

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

我这样写:

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.

使用yield语法,从evaluate获取返回的值是非常简单的,但没有它,我没有找到任何方法。

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.

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

UPDATE Wrote this thanks to the accepted answer and its comments. It uses 'Q' and works in node versions previous to 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
});


推荐答案

文档是可怕的,但似乎噩梦是基于可用性。我还没有在回调界面上找到很多信息,但是这也会导致一个缩进金字塔。

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.

所以你最好的办法是使用承诺,只需选择<一个href =https://promisesaplus.com/implementations>任何图书馆大致遵循ES6标准(它们都可用于非ES6环境)。

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).

您可以轻松地将您的线性生成器代码转换为承诺链,只需将 c 替换为然后 call:

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

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
});

这篇关于使用没有ES6语法和收益的Nightmare.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆