精華區beta Ruby 關於我們 聯絡資訊
出自我的Blog http://lightyror.blogspot.com/2006/09/feed-reader.html Ruby 下面要實做 RSS 2.0 Reader 實在相當的簡單 require 'rss/2.0' require 'open-uri' feed_url = 'http://blog.yam.com/twmovie/rss.xml' open(feed_url) do |feed| RSS::Parser.parse(feed.read , false).items.each do |item| puts 'Title ' + item.title puts 'Link ' + item.link puts 'Content ' + item.description puts 'Time ' + item.pubDate.to_s puts "\n\n" end end 上面這一段就是讀取相關的資訊 如果你只想取出前幾個 feed ,你可以使用 each_with_index require 'rss/2.0' require 'open-uri' feed_url = 'http://blog.yam.com/twmovie/rss.xml' @feed_contents = Array.new open(feed_url) do |feed| RSS::Parser.parse(feed.read , false).items.each_with_index do |item , i| @feed_contents << class="keyword">if i < class="keyword">end end 這段就是取出前三的 feed 的 content 放入 @feed_contents 這個 array 程式碼參考自此連結 http://www.robbyonrails.com/articles/2005/05/11/parsing-a-rss-feed PS. 文中的 RSS Feed http://blog.yam.com/twmovie/rss.xml 是一個極有意義的 Blog 因為有您 國片起飛 http://blog.yam.com/twmovie/ 支持 Ruby on Rails 也請支持優質國片 ATOM 1.0 Reader 是這樣做的 首先要先用 gem install atom 套件 gem install atom 然後再用 http / uri / atom 來 parse 我不確定 open-uri 可不可以使用 atom @@! require 'atom' require 'net/http' require 'uri' feed_url = 'http://blog.ning.com/atom.xml' Atom::Feed.new(Net::HTTP::get(URI::parse(feed_url))).entries.each do |entry| puts "'#{entry.title}' by #{entry.authors.first.name} on #{entry.published.strftime('%m/%d/%Y')}" puts "'#{entry.summary}'" puts "'#{entry.content.value}'" end 本程式參考自這個網頁 http://split-s.blogspot.com/2006/04/atom-10-parser-for-ruby.html -- lighty RoR 是一個介紹 lighttpd , SQLite , Ruby and Rails 的 Blog http://lightyror.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.218.90.242 ※ 編輯: giive 來自: 61.218.90.242 (09/21 13:12)