如何操作从X射线刮刀(node.js)检索的默认值

ces*_*uis 5 javascript scraper node.js

这是我的代码:

var Xray = require('x-ray');  
var x = Xray();
x('http://someurl.com', 'tr td:nth-child(2)', [{  
    text: 'a',
    url: 'a@href'
  }]).write('results.json')
Run Code Online (Sandbox Code Playgroud)

我需要使用每个标记中的第一个单词填充名为"text"的字段.标记值的示例:

"FirstWord SecondWord ThirdWord"

实际结果是文本:FirstWord SecondWord ThirdWord

期望的结果文本:FirstWord

我可以对result.json文件进行后处理,但我不喜欢这样.

Chr*_*iki 1

cbou 有一个 x 射线库的分支,
它的自定义 x 射线 API 有一个函数prepare,可以更改输出
https://github.com/cbou/x-ray#xrayprepare-str--fn

例子:

function uppercase(str) {
  return str.toUpperCase();
}

xray('mat.io')
.prepare('uppercase', uppercase)
.select('title | uppercase')
.run(function(err, title) {
  // title == MAT.IO
});
Run Code Online (Sandbox Code Playgroud)