【问题标题】:Cheerio find method not able to find when scrapingCheerio find 方法在抓取时无法找到
【发布时间】:2013-12-25 19:27:06
【问题描述】:

我正在使用以下代码

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 文件的结构告诉我不应该是这种情况。

【问题讨论】:

    标签: jquery node.js web-scraping cheerio


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 2018-11-08
      相关资源
      最近更新 更多