Ruby,使用Nokogiri和媒体:缩略图

时间:2010-12-10 21:55:52

标签: ruby-on-rails ruby rss nokogiri

我无休止地寻找解决方案来解决这个问题,当我想要显示图像时,我想我已经解决了这个问题。但是缩略图只是存储在根元素中的缩略图。很简单,这有效:

rss = Nokogiri::XML(open('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml'))
@news = rss.xpath('//item').map do |i|
  {
    'title'       => i.xpath('title').text, 
    'link'        => i.xpath('link').text, 
    'description' => i.xpath('description').text,
    'thumbnail'   => i.xpath('//media:thumbnail').attr('url')
  }
end

但编辑媒体:缩略图以引用该项目似乎打破了它:

{
  'title'       => i.xpath('title').text, 
  'link'        => i.xpath('link').text, 
  'description' => i.xpath('description').text,
  'thumbnail'   => i.xpath('media:thumbnail').attr('url')
}

我不明白为什么两个元素都是相同的。任何指向正确方向的人都会受到赞赏。

谢谢!

2 个答案:

答案 0 :(得分:1)

您的代码在第一个没有缩略图子元素的元素处断开。试试这个:

@news = rss.xpath('//item').map do |i|
  thumb = i.at_xpath('media:thumbnail').attr('url') if i.at_xpath('media:thumbnail')
  {
    'title' => i.at_xpath('title').text, 
    'link' => i.at_xpath('link').text, 
    'description' => i.at_xpath('description').text,
    'thumbnail' => thumb
  }
end

现在thumbnail将是URL(如果存在)或nil(如果不存在)。

答案 1 :(得分:0)

仅用于比较,以下是使用Nokogiri访问节点的一些替代方法:

require 'ap'
require 'open-uri'
require 'nokogiri'

rss = Nokogiri::XML(open('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml'))

# check for parsing errors...
puts "Errors exist" if (rss.errors.any?)

# use CSS accessors for simplicity...
news = rss.search('item').map { |i|

  # use CSS's namespace wildcard...
  thumbnail = i.at('media|thumbnail')
  begin
    url = thumbnail['url']
  rescue
    url = ''
  end
  {
    'title'       => i.search('title').text, 
    'link'        => i.search('link').text, 
    'description' => i.search('description').text,
    'thumbnail'   => url
  }
}

ap [*news[0 .. 1], *news[-2 .. -1]]
# >> [
# >>     [0] {
# >>               "title" => "Royal attack 'not down to radios'",
# >>                "link" => "http://www.bbc.co.uk/go/rss/int/news/-/news/uk-11975280",
# >>         "description" => "Police deny that a car carrying the Prince of Wales was caught up in student protests because of a breakdown in radio communications.",
# >>           "thumbnail" => "http://news.bbcimg.co.uk/media/images/50372000/jpg/_50372715_010819577-1.jpg"
# >>     },
# >>     [1] {
# >>               "title" => "Pope Anglican offer 'dented ties'",
# >>                "link" => "http://www.bbc.co.uk/go/rss/int/news/-/news/uk-11974543",
# >>         "description" => "Britain's ambassador to the Vatican feared a backlash over the Pope's invitation to Anglicans to switch churches, according to leaked US cables.",
# >>           "thumbnail" => "http://news.bbcimg.co.uk/media/images/48697000/jpg/_48697938_007958675-1.jpg"
# >>     },
# >>     [2] {
# >>               "title" => "Playing defence",
# >>                "link" => "http://news.bbc.co.uk/go/rss/int/news/-/1/hi/programmes/from_our_own_correspondent/9275324.stm",
# >>         "description" => "The friendly face of township where honeymoon bride was killed",
# >>           "thumbnail" => "http://news.bbcimg.co.uk/media/images/50385000/jpg/_50385343_football_afp.jpg"
# >>     },
# >>     [3] {
# >>               "title" => "Newspaper review",
# >>                "link" => "http://www.bbc.co.uk/go/rss/int/news/-/news/uk-11975338",
# >>         "description" => "Papers reflect on attack on royal car",
# >>           "thumbnail" => "http://news.bbcimg.co.uk/media/images/49254000/jpg/_49254932_efb006dc-dd09-47bd-8f2d-37752152f772.jpg"
# >>     }
# >> ]

请注意CSS名称空间通配符的使用。如果您不关心文档中的命名空间并希望处理所有内容,它可以使您的生活更轻松。