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}" }

Tagged: tutorial, rss, ruby


Nick said on June 21, 2007:

Thanks, so very very simple.

Trevor said on October 16, 2007:

Thank you, this is just the code snippet I need to begin pulling news headlines into an upcoming project.

neokain said on September 19, 2008:

simple than feedtools

doug said on January 23, 2009:

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.

Travis said on January 26, 2009:

@doug I agree that you shouldn't use this on every pageload. I use it in a nightly rake task that collects feeds.

feng said on August 14, 2009:

cool.....

Diego said on February 19, 2010:

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:




© 2010 Travis Roberts. All rights reserved.