Syed Bilal Haider
Syed Bilal Haider

Reputation: 45

How to web scrape IMDB movie rating

I am trying to scrape the the movie rating from IMDB website.
However I am getting this error:

AttributeError: 'NoneType' object has no attribute 'text'

when using this code:

rating_sauce = urllib.request.urlopen('https://www.imdb.com/title/tt1596343/?ref_=tt_urv')
 
rating_soup = bs.BeautifulSoup(rating_sauce, 'html.parser')
 
#container = rating_soup.find('div', {'class':'ipc-button__text'})
 
rating = rating_soup.find('span', {'class':'AggregateRatingButton__RatingScore-sc-11129m0-1'}).text

print("Movie Rating::" + rating)

Upvotes: 3

Views: 803

Answers (2)

Asif Shaikat
Asif Shaikat

Reputation: 11

You need to try changing the value of rating = rating_soup.find('span', {'class':'AggregateRatingButton__RatingScore-sc-11129m0-1'}).text by inspecting what is the current value against that specific element.

Here is my working code

import urllib.request
import bs4 as bs

rating_sauce = urllib.request.urlopen('https://www.imdb.com/title/tt1596343/?ref_=tt_urv')
rating_soup = bs.BeautifulSoup(rating_sauce, 'html.parser')
 
 
rating = rating_soup.find('span', {'class':'sc-7ab21ed2-1 jGRxWM'}).text

print("Movie Rating::" + rating)

with this output

Movie Rating::7.3

Upvotes: 1

Ram
Ram

Reputation: 4779

I don't understand why it's not working for you. Try this

import urllib.request
import bs4 as bs

rating_sauce = urllib.request.urlopen('https://www.imdb.com/title/tt1596343/?ref_=tt_urv')
rating_soup = bs.BeautifulSoup(rating_sauce, 'html.parser')
 
 
rating = rating_soup.find('span', {'class':'AggregateRatingButton__RatingScore-sc-1ll29m0-1'}).text

print("Movie Rating::" + rating)
Movie Rating::7.3

Upvotes: 0

Related Questions