idis
idis

Reputation: 27

Avoid similar names in the IMDb name search

My goal is to use the IMDb name search to get the ID for a person with exactly the name I'm looking for. Unfortunately, I also get all the similar names with this code.

import imdb

# creating instance of IMDb
ia = imdb.IMDb()

# list of names
names = ["Paul Alexander Abel", "Viktor Abel", "Heinz Abosch"]

# searching the name
for n in names:
    print("-----", n, "-----")
    search = ia.search_person(n)

# loop for printing the name and id
    for i in range(len(search)):
    # getting the id
        id = search[i].personID
        print(search[i]['name'] + " : " + id)
# output:
----- Viktor Abel -----
Viktor Vrabec : 0903968
Viktor Brabec : 0102632
Viktor Abroi : 6636627
Viktor Absolutov : 5153732
Viktoriia Bessarab : 10441797
Viktoriya Brabec : 13413004
Viktor Abolduyev : 0008850
Viktor Abramov : 12453191
Viktor Abramov : 11944753
Viktor Abakumov : 12360818
Viktor Abramovskiy : 3277781
Viktor Abramyan : 8311765
Viktor Abrosimov : 1480489
Viktoriya Belyakova : 11558430
Viktoriya Belova : 1495496
Viktoriya Belyaeva : 5274884
Viktoria Believein : 3637309
Viktoria Belovonskaya : 10260945
Viktoriya Belyavskaya : 10267906
Viktorie Isabella Ter-Akopow : 9422886
----- Heinz Abosch -----
Heinz Ambrosch : 0024376

Is there a way to change that?

Upvotes: 0

Views: 89

Answers (1)

Corralien
Corralien

Reputation: 120519

Limit the number of result returned:

# The first result is the best match
search = ia.search_person(n, results=1)[0]

Your loop:

for n in names:
    print("-----", n, "-----")
    search = ia.search_person(n, results=1)[0]
    if search['name'].lower() == n.lower():
        id = search.personID
        print(search['name'] + " : " + id)

Upvotes: 0

Related Questions