문제

I am using the following code

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']);
    });
});

And my problem is, why can't the third line in the .each loop be

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

If I use that line the output is empty but the structure of the downloaded xml file tells me that should not be the case.

도움이 되었습니까?

해결책

Re-configure the cheerio object adding lowerCaseTags flag;

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

Now console.log($(xmlItem).find('pubDate').text()); should work fine.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top