0

The Python imdbpy module allows you search by movie title.

Example:


import imdb


ia = imdb.IMDb()

search_movie = ia.search_movie('Ready Player One')

Result:


[<Movie id:1677720[http] title:_Ready Player One (2018)_>, <Movie id:8183756[http] title:_"Projector" Ready Player One (2018)_>, <Movie id:13695838[http] title:_"Big Pauly Talks Movies" Ready Player One (2018)_>, <Movie id:8045574[http] title:_"Ready Player One LIVE at SXSW" (2018)_>, ...]

How do grab the first Movie id from the results?

Tried:

  • While print(search_movie[0]['title']) brings back the title, these do not work:

    • print(search_movie[0]['Movie id'])
    • print(search_movie[0]['id'])
SeaDude
  • 3,725
  • 6
  • 31
  • 68

2 Answers2

1

A quick check of the source code shows that you want print(search_movie[0]['movieID']) or print(search_movie[0].getID().

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thank you. I'll work at reading source. Did you look [here](https://github.com/alberanid/imdbpy/blob/c416d46389465b61ed82969a6a132b72f829aac2/bin/search_movie.py#L49)? – SeaDude Nov 21 '21 at 05:20
  • Ah, now I see. [REF1](https://github.com/alberanid/imdbpy/blob/c416d46389465b61ed82969a6a132b72f829aac2/imdb/Movie.py#L214), [REF2](https://github.com/alberanid/imdbpy/blob/c416d46389465b61ed82969a6a132b72f829aac2/imdb/Movie.py#L93) – SeaDude Nov 21 '21 at 05:26
1

Assuming this is pure Python and not Xytron, Your search is returning a variable of type list and, as you found, the first result can be isolated with...

Try print(search_movie[0])

The contents of search_movie[0] is a simple text string. A way is to grab the digits between id: And [http]

If the IMDb module cannot give you the exact format you want, you can do manipulate the result with a find to get the position of the [http] and assume the start of a cut will be the character number 10 (starting with 0).

endCut = search_movie[0].find("[http]")

MovieID= search_movie[0][10, endCut]

MovieID will be a string. To make it an integer, you'll need to do MovieID= int(search_movie[0][10, endCut]) in the string where or you can do it with

SeaDude
  • 3,725
  • 6
  • 31
  • 68
GT Electronics
  • 146
  • 1
  • 9