【问题标题】:Xray Selector not returning valueX射线选择器不返回值
【发布时间】:2015-07-15 16:52:50
【问题描述】:

我有以下运行最新版本的 x-ray npm 模块的 sn-p。

我希望 Meta 和 Metatags 元素被填充,但当我打印出 obj 时它们不是。我做错了什么?

var Xray = require('x-ray');
var x = Xray();

x('http://www.rte.ie', {
    title: 'title',
    metatags: x('meta', [{
        name: 'meta@name',
        description: 'meta@content'
  }]),
    meta: 'meta'
})(function (err, obj) {
    console.log(obj);
})

【问题讨论】:

    标签: javascript node.js web-scraping npm cheerio


    【解决方案1】:

    为 meta 设置选择器的方式,本质上是让 xray 寻找:

    <meta>
        <meta name="@val" />
        <meta description="@val" />
        <meta name="@val" />
        <meta description="@val" />
    </meta>
    

    因此,同样的替代方案是:

    x('http://www.rte.ie', {
        title: 'title',
        metaname: 'meta@name',
        metadescription: 'meta@content'
      }]),
        meta: 'meta'
    })(function (err, obj) {
        console.log(obj);
    })
    

    如果您要查找多个名称/描述,只需将整个内容放在方括号中即可。

    那个,或者搜索使用head标签作为选择器:

    x('http://www.rte.ie', 'html'{
        title: 'title',
        meta: x('head', [{
            name: 'meta@name',
            description: 'meta@content'
        }])
      }]),
        meta: 'meta'
    })(function (err, obj) {
        console.log(obj);
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      相关资源
      最近更新 更多