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 ループの 3 行目を

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

その行を使用すると、出力は空になりますが、ダウンロードされた xml ファイルの構造から、そうではないことがわかります。

4

1 に答える 1

1

lowerCaseTagsフラグを追加するチェリオ オブジェクトを再構成します。

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

console.log($(xmlItem).find('pubDate').text());はうまくいくはずです。

于 2013-12-08T19:45:54.723 に答える