Last active
April 15, 2023 07:57
-
-
Save jayrambhia/1678382 to your computer and use it in GitHub Desktop.
Fetch movie information from IMDB using Python!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Author : Jay Rambhia | |
Git : https://github.com/jayrambhia | |
gist : https://gist.github.com/jayrambhia | |
''' | |
import urllib2 | |
from BeautifulSoup import BeautifulSoup | |
from mechanize import Browser | |
import re | |
def getunicode(soup): | |
body='' | |
if isinstance(soup, unicode): | |
soup = soup.replace(''',"'") | |
soup = soup.replace('"','"') | |
soup = soup.replace(' ',' ') | |
body = body + soup | |
else: | |
if not soup.contents: | |
return '' | |
con_list = soup.contents | |
for con in con_list: | |
body = body + getunicode(con) | |
return body | |
def main(): | |
movie = str(raw_input('Movie Name: ')) | |
movie_search = '+'.join(movie.split()) | |
base_url = 'http://www.imdb.com/find?q=' | |
url = base_url+movie_search+'&s=all' | |
title_search = re.compile('/title/tt\d+') | |
br = Browser() | |
br.set_proxies({'http':'http://username:password@proxy:port', | |
'https':'https://username:password@proxy:port'}) | |
br.open(url) | |
link = br.find_link(url_regex = re.compile(r'/title/tt.*')) | |
res = br.follow_link(link) | |
soup = BeautifulSoup(res.read()) | |
movie_title = getunicode(soup.find('title')) | |
rate = soup.find('span',itemprop='ratingValue') | |
rating = getunicode(rate) | |
actors=[] | |
actors_soup = soup.findAll('a',itemprop='actors') | |
for i in range(len(actors_soup)): | |
actors.append(getunicode(actors_soup[i])) | |
des = soup.find('meta',{'name':'description'})['content'] | |
genre=[] | |
infobar = soup.find('div',{'class':'infobar'}) | |
r = infobar.find('',{'title':True})['title'] | |
genrelist = infobar.findAll('a',{'href':True}) | |
for i in range(len(genrelist)-1): | |
genre.append(getunicode(genrelist[i])) | |
release_date = getunicode(genrelist[-1]) | |
print movie_title,rating+'/10.0' | |
print 'Relase Date:',release_date | |
print 'Rated',r | |
print '' | |
print 'Genre:', | |
print ', '.join(genre) | |
print '\nActors:', | |
print ', '.join(actors) | |
print '\nDescription:' | |
print des | |
if __name__ == '__main__': | |
main() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Author : Jay Rambhia | |
Git : https://github.com/jayrambhia | |
gist : https://gist.github.com/jayrambhia | |
''' | |
import urllib2 | |
from BeautifulSoup import BeautifulSoup | |
from mechanize import Browser | |
import re | |
def getunicode(soup): | |
body='' | |
if isinstance(soup, unicode): | |
soup = soup.replace(''',"'") | |
soup = soup.replace('"','"') | |
soup = soup.replace(' ',' ') | |
body = body + soup | |
else: | |
if not soup.contents: | |
return '' | |
con_list = soup.contents | |
for con in con_list: | |
body = body + getunicode(con) | |
return body | |
def main(): | |
movie = str(raw_input('Movie Name: ')) | |
movie_search = '+'.join(movie.split()) | |
base_url = 'http://www.imdb.com/find?q=' | |
url = base_url+movie_search+'&s=all' | |
title_search = re.compile('/title/tt\d+') | |
br = Browser() | |
br.set_proxies({'http':'http://username:password@proxy:port', | |
'https':'https://username:password@proxy:port'}) | |
br.open(url) | |
link = br.find_link(url_regex = re.compile(r'/title/tt.*')) | |
res = br.follow_link(link) | |
soup = BeautifulSoup(res.read()) | |
movie_title = getunicode(soup.find('title')) | |
rate = soup.find('span',itemprop='ratingValue') | |
rating = getunicode(rate) | |
actors=[] | |
actors_soup = soup.findAll('a',itemprop='actors') | |
for i in range(len(actors_soup)): | |
actors.append(getunicode(actors_soup[i])) | |
des = soup.find('meta',{'name':'description'})['content'] | |
genre=[] | |
infobar = soup.find('div',{'class':'infobar'}) | |
r = infobar.find('',{'title':True})['title'] | |
genrelist = infobar.findAll('a',{'href':True}) | |
for i in range(len(genrelist)-1): | |
genre.append(getunicode(genrelist[i])) | |
release_date = getunicode(genrelist[-1]) | |
print movie_title,rating+'/10.0' | |
print 'Relase Date:',release_date | |
print 'Rated',r | |
print '' | |
print 'Genre:', | |
print ', '.join(genre) | |
print '\nActors:', | |
print ', '.join(actors) | |
print '\nDescription:' | |
print des | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remade after IMDb changed the way their page works
https://gist.github.com/Dob-The-Duilder/98f0765ce6dd9c1e11c3a649619654ac