Skip to content

Instantly share code, notes, and snippets.

@etscrivner
Created March 25, 2010 06:32
Show Gist options
  • Save etscrivner/343259 to your computer and use it in GitHub Desktop.
Save etscrivner/343259 to your computer and use it in GitHub Desktop.
# topmovies.rb - Scrape top grossing movies and their ratings from rotten tomatoes
require 'rubygems'
require 'hpricot'
require 'open-uri'
class RottenTomatoes
def initialize
@url = "http://www.rottentomatoes.com"
@hp = Hpricot(open(@url))
end
def top_five_movies
titles = (@hp/"table//td.boxofficeTbl_movieCol/a").collect{|title|
title.inner_text.strip
}
grosses = (@hp/"table//td.boxofficeTbl_grossCol/a").collect{|gross|
gross.inner_text.strip
}
ratings = (@hp/"table//td.boxofficeTbl_tomatometerCol/a/span/span.label").collect{|rating|
rating.inner_text.strip
}
result = []
i = 0;
titles.each do |title|
result[i] = {}
result[i]['title'] = title
i += 1
end
i = 0
grosses.each do |gross|
result[i]['gross'] = gross
i += 1
end
i = 0
ratings.each do |rating|
result[i]['rating'] = rating
i += 1
end
result
end
end
rt = RottenTomatoes.new
top = rt.top_five_movies
printf("%-6s | %-25s | %-5s\n", "Rating", "Title", "Gross");
44.times { print "-" }
print "\n"
top.each do |movie|
printf("%-6s | %-25s | %-5s\n", movie['rating'], movie['title'], movie['gross'])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment