1

我正在尝试将 NightmareJS v2 和 Vo 与 Node.JS 一起使用来浏览一些搜索词并从中收集数据。我的代码如下所示:

const nightmare = require('nightmare'),
  vo = require('vo'),
  nbot = nightmare({ title: 'Bot',
                        show: true });

const searchTerms = ['spacex', 'tesla', 'elon musk', 'hyperloop']; // EXAMPLE SEARCH TERMS

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

function * run() {
  yield nbot.goto('http://google.com');

  yield * forEach(searchTerms, gen);

  yield nbot.end()
  .then(function(result) {
    console.log(result) // STUFF SHOULD BE LOGGED HERE
  });
}

function * gen(item) {
  yield nbot.wait('input[title="Search"]')
            .click('input[title="Search"]')
            .type('input[title="Search"]', item)
            .click('input[name="btnK"]')
            .wait(100)
            .screenshot(item + '.png')
            .insert('input[title="Search"]', '')
            .evaluate(function() {
              return 'foobar' // STUFF RETURNED HERE
            })
}

function * forEach (arr, fn) { // NEEDED BECAUSE FOREACH DOESN'T WORK IN GENERATORS
  let i;
  for (i = 0; i < arr.length; i++) {
    yield * fn(arr[i]);
  }
}

根据 NightmareJS 的文档,如果你在里面返回一些东西,evaluate那么当你使用then. 当我尝试这个时,我变得不确定。我认为它与发电机有关,但我对它们很陌生,所以我不能说。帮助表示赞赏。

4

2 回答 2

3

该示例很接近,但看起来您正在使用 Promise 语法混合和匹配生成器。该.evaluate()方法的行为与您描述的一样,但是当您使用vo(or co) 时,.then()将为您处理和管理,如果定义了一个,则返回表达式.then()左侧的值。yield此外,对于它的价值,您的生成器不会返回值,因此您将(正确地)得到undefined.

将更深入地介绍迭代器、生成器、承诺和vo/ co。您可能还想查看nightmare-examples有关vo.

最后,您的示例已修改为从gen,到forEach至返回结果run

const nightmare = require('nightmare'),
  vo = require('vo'),
  nbot = nightmare({
    title: 'Bot',
    show: true
  });

const searchTerms = ['spacex', 'tesla', 'elon musk', 'hyperloop']; // EXAMPLE SEARCH TERMS

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

function* run() {
  yield nbot.goto('http://google.com');

  var results = yield * forEach(searchTerms, gen);
  console.log(results);
  yield nbot.end();
}

function* gen(item) {
  var value = yield nbot.wait('input[title="Search"]')
    .click('input[title="Search"]')
    .type('input[title="Search"]', item)
    .click('input[name="btnK"]')
    .wait(100)
    .screenshot(item + '.png')
    .insert('input[title="Search"]', '')
    .evaluate(function() {
      return 'foobar' // STUFF RETURNED HERE
    });

  return value;
}

function* forEach(arr, fn) { // NEEDED BECAUSE FOREACH DOESN'T WORK IN GENERATORS
  let i;
  var results = [];
  for (i = 0; i < arr.length; i++) {
    results.push(yield * fn(arr[i]));
  }
  return results;
}
于 2016-06-02T02:52:22.460 回答
0

很好的例子!默认情况下,提供的示例在我的情况下不起作用(来自德国的 IP)。

这里对一般搜索进行了一些修复:

const nightmare = require('nightmare'),
  vo = require('vo'),
  nbot = nightmare({
    title: 'Bot',
    show: true
  });

const searchTerms = ['spacex', 'tesla', 'elon musk', 'hyperloop']; // EXAMPLE SEARCH TERMS

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

function* run() {
  yield nbot.goto('http://google.com');

  var results = yield * forEach(searchTerms, gen);
  console.log(results);
  yield nbot.end();
}

function* gen(item) {
  var inputTitle
  var value = yield nbot
    .evaluate(() => {
      return document.getElementById("lst-ib").title
    })
    .then((lang) => {
      inputTitle = 'input[title="' + lang + '"]'
      return true
    });
  yield nbot
    .wait(inputTitle)
    .click(inputTitle)
    .type(inputTitle, item)
    .click('input[name="btnK"]')
    .wait(100)
    .screenshot(item + '.png')
    .insert(inputTitle, '')
    .evaluate(function() {
      return 'foobar' // STUFF RETURNED HERE
    });

  return value;
}

function* forEach(arr, fn) { // NEEDED BECAUSE FOREACH DOESN'T WORK IN GENERATORS
  let i;
  var results = [];
  for (i = 0; i < arr.length; i++) {
    results.push(yield * fn(arr[i]));
  }
  return results;
}
于 2017-07-16T11:42:31.883 回答