不能 return document.querySelector 在 node.js 上进行噩梦 API

cannot return document.querySelector with nightmare API on node.js

我正在使用 nightmare.js for web scripting on a Chinese e-commerce website taobao(www.taobao.com/). The goal is to get product information. The code is very similar to the yahoo example code,但结果始终为空。我试图将 console.log 调试并意识到错误可能在于 querySelector。 如果有人有时间看一下,下面是代码。非常感谢。

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

nightmare
  .goto('https://www.taobao.com')
  .type('form[action*="/search"] [name=q]', 'hellow kitty')
  .click('form[action*="/search"] [type=submit]')
  .wait(2000)
  .evaluate(function () {
    return document.querySelector('.row.row-2.title a')
  })
  .end()
  .then(function (result) {
    console.log(result)
  })
  .catch(function (error) {
    console.error('Search failed:', error);
  });

首先也是最重要的:仔细检查您的选择器并确保它们存在。

撇开这个不谈,问题在于return document.querySelector('.row.row-2.title a')。 DOM 列表(如从 document.querySelector 返回的列表)不能很好地序列化。在返回之前尝试拉出你想要的结果 - 假设你想要来自锚点的 HREF,你可以做一些事情(从臀部):

return Array.from(document.querySelector('.row.row-2.title a')).map(a=> a.href);