0

我正在使用以下代码

var request = require('request');
var cheerio = require('cheerio');
var url = "http://www.mbl.is/feeds/fp/";
request(url, function(err, resp, body) {
    if (err)
        throw err;
    $ = cheerio.load(body,{xmlMode : true});
    $('item').each(function(item, xmlItem){
        console.log($(xmlItem).find('title').text());
        console.log($(xmlItem).find('link').text());
        console.log($(xmlItem).children()[3]['children'][0]['data']);
    });
});

我的问题是,为什么 .each 循环中的第三行不能是

console.log($(xmlItem).find('pubDate').text());

如果我使用该行,则输出为空,但下载的 xml 文件的结构告诉我不应该是这种情况。

4

1 回答 1

1

重新配置cheerio对象添加lowerCaseTags标志;

$ = cheerio.load(body, {
  xmlMode: true,
  lowerCaseTags: true
});

现在console.log($(xmlItem).find('pubDate').text());应该可以正常工作了。

于 2013-12-08T19:45:54.723 回答