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

4
  • 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 stackoverflow.com/questions/24898797/… Commented Apr 4, 2023 at 13:49
  • it print out the movie name but still get the key error Commented Apr 4, 2023 at 14:14
  • Apparently search_movie() must return a list of strings (movie names) not movie data... Commented Apr 4, 2023 at 14:18
  • @JonSG so how can i fix it Commented Apr 4, 2023 at 14:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.