0

So i wanted to get the year of the movie in IMdPY midule using search_movie() like this

import imdb

ia = imdb.IMDb()

search = ia.search_movie("The Shawshank Redemption")

year = search[0]['year']

print(search['title'] + " : " + str(year))

And always it gives me this ERR

KeyError: 'year'

though this err doesn't come up when i use ia.get_movie('0133093') (which is by searching ID)... and i dont always know the id of the movie so i want to know if there is any way i can use the ['year'] in search_movie func

  • 1
    What if you `print(search[0])` right before that line? Though if you believe that `search[0]` returns "some movie" but one that may or may not have a valid `"year"` then you might check out if this helps solve things for you https://stackoverflow.com/questions/24898797/check-if-key-exists-and-iterate-the-json-array-using-python – JonSG Apr 04 '23 at 13:49
  • it print out the movie name but still get the key error – Prison Mike Apr 04 '23 at 14:14
  • Apparently `search_movie()` must return a list of strings (movie names) not movie data... – JonSG Apr 04 '23 at 14:18
  • @JonSG so how can i fix it – Prison Mike Apr 04 '23 at 14:50

1 Answers1

0

Thank you @JonSG and others i found a way to work on it. the key is getting the Movie ID from search results and use that ID in get_movie()... since that works fine

import imdb

ia = imdb.IMDb()

name = "The Shawshank Redemption"
search = ia.search_movie(name)

id = search[0].movieID

search_by_id = ia.get_movie(id)
year = search_by_id['year']

print(search_by_id['title'] + " : " + str(year))

and the output will be

The Shawshank Redemption : 1994

Thanks