Reputation: 4375
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'])
Upvotes: 0
Views: 1669
Reputation: 146
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
Upvotes: 1
Reputation: 54698
A quick check of the source code shows that you want print(search_movie[0]['movieID'])
or print(search_movie[0].getID()
.
Upvotes: 1