2011-05-09 33 views
0

我試圖使用Nokogiri從YouTube的XML Atom提要中提取網址。如何使用Nokogiri從Atom訂閱源訪問URL?

我有一些運氣提取id的yay命名空間,但很難提取URL。例如,YouTube的API提供了三種不同的<media:thumbnail>標籤和三種不同的<media:content>標籤。您可以在下面看到,這些標記中沒有顯示這些網址。我的目標是分別從第一個<media:thumbnail><media:content>中提取URL。

這裏是我的代碼pastie:http://pastie.org/1881669

這是在終端輸出的一個條目:

{:group=>\"ComedyThe OMG Cat or the WTF cat - funny gobsmacked cat. The cats name is \\\"Choco\\\" and if i told you what she was looking at, I would have to kill you!!!The OMG Cat, omg cat, wtf cat, cat, cats, cat fail, the wtf cat, cute cats, cute animals, funny cats, funny cat video, omg, wtf, gobsmacked cat, gobsmacked, two girls one cup, reactionThe OMG Cat\", :category=>\"Comedy\", :content=>\"\", :description=>\"The OMG Cat or the WTF cat - funny gobsmacked cat. The cats name is \\\"Choco\\\" and if i told you what she was looking at, I would have to kill you!!!\", :keywords=>\"The OMG Cat, omg cat, wtf cat, cat, cats, cat fail, the wtf cat, cute cats, cute animals, funny cats, funny cat video, omg, wtf, gobsmacked cat, gobsmacked, two girls one cup, reaction\", :player=>\"\", :thumbnail=>\"\", :title=>\"The OMG Cat\"}]"

+1

你能提供一段XML文檔的? – taro 2011-05-15 08:21:51

+0

如果你編輯你的問題並在這裏提供你的代碼的工作部分,而不是指望我們追查它,這將有所幫助。如果這個餡餅消失了,它也會留在你的問題中,而不會死。而且,我會再次提供@ taro的請求,提供XML的簡化示例。沒有這個,我們無法幫助你。 – 2011-05-17 14:56:28

回答

3

從頭開始,做(這個特殊的情況下,尋找一個YouTube播放列表XML飼料,但我相信你可以爲任何視頻輸入做相同):

pid='5ABDCC8D096B0853' #requires a playlist id to lookup all its entries 
=> "5ABDCC8D096B0853" 
doc = Nokogiri::XML(open("http://gdata.youtube.com/feeds/api/playlists/#{pid}?v=2")) 

現在你有引入nokogiri XML包含在doc變量中的文檔。從那裏,你可以得到所有媒體:內容和媒體:縮略圖節點集。一旦獲得了一個節點集,就可以像第一個元素一樣訪問第一個節點集。

doc.xpath('//media:content')[0] 
=> #<Nokogiri::XML::Element:0x82dd58d8 name="content" namespace=#<Nokogiri::XML::Namespace:0x82dd5ea0 prefix="media" href="http://search.yahoo.com/mrss/"> attributes=[#<Nokogiri::XML::Attr:0x82dd5770 name="url" value="http://www.youtube.com/ep.swf?id=5ABDCC8D096B0853">, #<Nokogiri::XML::Attr:0x82dd575c name="type" value="application/x-shockwave-flash">, #<Nokogiri::XML::Attr:0x82dd5748 name="format" namespace=#<Nokogiri::XML::Namespace:0x82dd3d44 prefix="yt" href="http://gdata.youtube.com/schemas/2007"> value="5">]> 

doc.xpath('//media:content')[0]['url'] 

=> "http://www.youtube.com/ep.swf?id=5ABDCC8D096B0853" 

並做縮略圖是相同的:

doc.xpath('//media:thumbnail')[0]['url'] 
=> "http://i.ytimg.com/vi/eBefgm7hdpU/default.jpg" 
相關問題