2010-12-10 24 views
2

我已經無休止地搜索這個解決方案,我想我已經解決了它,當我有一個圖像顯示。但是縮略圖只是存儲在根元素中的縮略圖。很簡單這個工程:紅寶石,使用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') 
} 

我不明白,爲什麼作爲兩個元素是相同的。任何指針正確的方向將不勝感激。

謝謝!

+0

沒有輸入示例和XPath表達式問題,這不是'xpath'問題,而是特定語言(ruby)類的方法問題。 – 2010-12-10 22:42:04

回答

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,如果它沒有。

+0

嘿!非常感謝,我認爲每個人都有一個縮略圖屬性,但顯然不是,很好的解決方案,現在一切都很完美:D – 2010-12-11 00:18:16

+0

爲防萬一以後有人發現這種情況,我必須添加:除非!thumb.nil ? \t thumb ='missing.jpg' \t end – 2010-12-12 16:07:53

+0

這也可以寫成更短作爲 'thumbnail'=> thumb || 'missing.jpg' – 2010-12-13 10:56:42

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命名空間通配符。如果您不關心文檔中的命名空間並想要處理所有內容,它可以使您的生活變得更加輕鬆。

+0

謝謝你,幫助我認識到有多少選擇,出於某種原因,這對我沒有用,儘管我還是得到零錯誤。 – 2010-12-12 16:07:28