Archive
Get the RottenTomatoes rating of a movie
Problem
In the previous post we saw how to extract the IMDB rating of a movie. Now let’s see the same thing with the RottenTomatoes website. Their rating looks like this:

Solution
Download link: https://github.com/jabbalaci/Movie-Ratings. Source code:
#!/usr/bin/env python # RottenTomatoesRating # Laszlo Szathmary, 2011 ([email protected]) from BeautifulSoup import BeautifulSoup import sys import re import urllib import urlparse class MyOpener(urllib.FancyURLopener): version = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15' class RottenTomatoesRating: # title of the movie title = None # RT URL of the movie url = None # RT tomatometer rating of the movie tomatometer = None # RT audience rating of the movie audience = None # Did we find a result? found = False # for fetching webpages myopener = MyOpener() # Should we search and take the first hit? search = True # constant BASE_URL = 'http://www.rottentomatoes.com' SEARCH_URL = '%s/search/full_search.php?search=' % BASE_URL def __init__(self, title, search=True): self.title = title self.search = search self._process() def _search_movie(self): movie_url = "" url = self.SEARCH_URL + self.title page = self.myopener.open(url) result = re.search(r'(/m/.*)', page.geturl()) if result: # if we are redirected movie_url = result.group(1) else: # if we get a search list soup = BeautifulSoup(page.read()) ul = soup.find('ul', {'id' : 'movie_results_ul'}) if ul: div = ul.find('div', {'class' : 'media_block_content'}) if div: movie_url = div.find('a', href=True)['href'] return urlparse.urljoin( self.BASE_URL, movie_url ) def _process(self): if not self.search: movie = '_'.join(self.title.split()) url = "%s/m/%s" % (self.BASE_URL, movie) soup = BeautifulSoup(self.myopener.open(url).read()) if soup.find('title').contents[0] == "Page Not Found": url = self._search_movie() else: url = self._search_movie() try: self.url = url soup = BeautifulSoup( self.myopener.open(url).read() ) self.title = soup.find('meta', {'property' : 'og:title'})['content'] if self.title: self.found = True self.tomatometer = soup.find('span', {'id' : 'all-critics-meter'}).contents[0] self.audience = soup.find('span', {'class' : 'meter popcorn numeric '}).contents[0] if self.tomatometer.isdigit(): self.tomatometer += "%" if self.audience.isdigit(): self.audience += "%" except: pass if __name__ == "__main__": if len(sys.argv) == 1: print "Usage: %s 'Movie title'" % (sys.argv[0]) else: rt = RottenTomatoesRating(sys.argv[1]) if rt.found: print rt.url print rt.title print rt.tomatometer print rt.audience
Usage:
The constructor has an optional parameter, which is True by default (search=True). It means that first we use the search function of the RT website and then we try to follow the first link. If search=False, the script tries to access the movie page directly. If it fails, then it falls back to the first case, i.e. it will try to find the movie via search.
Which version is better? It depends :) If there are several movies with the same title, then with search=True you will get the latest movie. If search=False, then you will usually get the oldest movie with that title.
For instance, for me “Star Wars” means episode 4, thus with the title “star wars”, search=False will return the relevant hit. But for “up in the air”, I would like to get the movie from 2009, not from 1940, thus in this case search=True would be better.
If you are in doubt, use the default case, i.e. search=True.
Related links
Update (20110329):
You will find the latest version of the script at https://github.com/jabbalaci/Movie-Ratings.
[ @reddit ]
Get the IMDB rating of a movie
Problem
You want to get the IMDB rating of a movie. For instance, you have a large collection of movies, and you want to figure out their ratings. An IMDB rating looks like this:
Solution
Here is a script that extracts the rating of a movie from IMDB. The script was inspired by the work of Rag Sagar.
Download link: https://github.com/jabbalaci/Movie-Ratings. Source code:
#!/usr/bin/env python
# ImdbRating
import os
import sys
import re
import urllib
import urlparse
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
class MyOpener(urllib.FancyURLopener):
version = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15'
class ImdbRating:
# title of the movie
title = None
# IMDB URL of the movie
url = None
# IMDB rating of the movie
rating = None
# Did we find a result?
found = False
# constant
BASE_URL = 'http://www.imdb.com'
def __init__(self, title):
self.title = title
self._process()
def _process(self):
movie = '+'.join(self.title.split())
br = Browser()
url = "%s/find?s=tt&q=%s" % (self.BASE_URL, movie)
br.open(url)
if re.search(r'/title/tt.*', br.geturl()):
self.url = "%s://%s%s" % urlparse.urlparse(br.geturl())[:3]
soup = BeautifulSoup( MyOpener().open(url).read() )
else:
link = br.find_link(url_regex = re.compile(r'/title/tt.*'))
res = br.follow_link(link)
self.url = urlparse.urljoin(self.BASE_URL, link.url)
soup = BeautifulSoup(res.read())
try:
self.title = soup.find('h1').contents[0].strip()
self.rating = soup.find('span',attrs='rating-rating').contents[0]
self.found = True
except:
pass
# class ImdbRating
if __name__ == "__main__":
if len(sys.argv) == 1:
print "Usage: %s 'Movie title'" % (sys.argv[0])
else:
imdb = ImdbRating(sys.argv[1])
if imdb.found:
print imdb.url
print imdb.title
print imdb.rating
Related links
- Get the RottenTomatoes rating of a movie
- IMDbPY provides lots of possibilities if you want to retrieve and manage IMDB data. Undoubtedly, IMDbPY is a more professional (but heavier) solution. Can be installed via PyPI.
Update (20110329):
You will find the latest version of the script at https://github.com/jabbalaci/Movie-Ratings.
[ @reddit ]
Related posts (update 20120222)
- Get IMDB ratings without any scraping (at the end of this post you’ll find a much shorter Python code too)
Blog Stats
- 1,652,053 hits
Random Post
Recent Posts
Archives
- December 2025 (1)
- December 2024 (2)
- March 2024 (1)
- February 2024 (3)
- June 2023 (1)
- December 2022 (1)
- May 2022 (1)
- March 2022 (1)
- February 2022 (2)
- October 2020 (3)
- September 2020 (2)
- July 2020 (1)
- December 2019 (1)
- November 2019 (7)
- June 2019 (2)
- May 2019 (1)
- February 2019 (1)
- January 2019 (1)
- August 2018 (1)
- July 2018 (4)
- June 2018 (5)
- May 2018 (1)
- March 2018 (2)
- February 2018 (1)
- January 2018 (2)
- December 2017 (2)
- October 2017 (1)
- August 2017 (1)
- May 2017 (2)
- April 2017 (1)
- March 2017 (1)
- February 2017 (3)
- January 2017 (5)
- December 2016 (3)
- October 2016 (2)
- September 2016 (1)
- August 2016 (4)
- July 2016 (3)
- June 2016 (2)
- May 2016 (2)
- April 2016 (1)
- March 2016 (4)
- February 2016 (1)
- January 2016 (4)
- December 2015 (5)
- November 2015 (8)
- October 2015 (2)
- September 2015 (3)
- August 2015 (6)
- July 2015 (4)
- June 2015 (1)
- May 2015 (3)
- April 2015 (2)
- February 2015 (1)
- January 2015 (6)
- December 2014 (5)
- November 2014 (2)
- October 2014 (1)
- September 2014 (1)
- August 2014 (14)
- July 2014 (3)
- June 2014 (6)
- May 2014 (3)
- April 2014 (2)
- March 2014 (4)
- February 2014 (3)
- January 2014 (19)
- December 2013 (8)
- November 2013 (9)
- October 2013 (4)
- September 2013 (10)
- August 2013 (16)
- July 2013 (4)
- June 2013 (7)
- May 2013 (7)
- April 2013 (3)
- March 2013 (12)
- February 2013 (2)
- January 2013 (10)
- December 2012 (18)
- November 2012 (3)
- October 2012 (4)
- September 2012 (5)
- August 2012 (1)
- July 2012 (1)
- May 2012 (8)
- April 2012 (9)
- March 2012 (17)
- February 2012 (3)
- January 2012 (8)
- November 2011 (11)
- October 2011 (7)
- September 2011 (17)
- August 2011 (5)
- June 2011 (1)
- May 2011 (7)
- April 2011 (21)
- March 2011 (21)
- February 2011 (7)
- December 2010 (4)
- November 2010 (1)
- October 2010 (16)
- September 2010 (15)

You must be logged in to post a comment.