Parsing an RSS feed with Ruby
JUN
09
2007
Parsing an RSS feed is insanely simple with Ruby. Two lines is all it takes. . .
require 'rss'
rss = RSS::Parser.parse(open('http://www.travisonrails.com/feed/posts').read, false)
Now you'll have an Array of the results, so you can do something like:
rss.items.each { |i| puts "#{i.title} - #{i.date}" }
Thank you, this is just the code snippet I need to begin pulling news headlines into an upcoming project.
simple than feedtools
This does nothing for caching the rss content though. If you embed this on say your home page then you'll be hitting the rss feed on every page load. Not very kind to the poor rss feed. I think that's the point of feedtools: to integrate http caching with RSS parsing.
@doug I agree that you shouldn't use this on every pageload. I use it in a nightly rake task that collects feeds.
cool.....
You could also do this: #!/usr/bin/env ruby require 'rss' rss = RSS::Parser.parse('http://feeds.sfgate.com/sfgate/rss/feeds/bayarea', false) rss.items.each { |i| puts "#{i.title} - #{i.date}" } No need for the open().read part
Leave a comment:
Popular Posts
Search
Tags
actionmailer activerecord ajax apache apple barcamp caching capistrano centos code golf css db delete eager loading ebay email attachment erb flash ftp fun generators get haml helpers ie sucks javascript jquery lightbox lost merb net ftp paperclip passenger php plexus post presentation rails rails machine railsconf redesign rest rjs routes rss ruby ruby on rails safari script sinatra symfony text replacement tips tutorial twitter xhtml
Projects
© 2010 Travis Roberts. All rights reserved.
Nick said on June 21, 2007:
Thanks, so very very simple.