Python Forum
IMDbPy "search_person" result cant be found in actors list in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IMDbPy "search_person" result cant be found in actors list in Python
#1
Have a list of actors. Asking the user to enter an actor name that exists in the list. Although entering an existing name in the list, the code says it does not exist in the list. The "counter" function shows 0, "boolean" operator is False.

Why getting this error ?

note: If I enter the actor name manually, the code can find the name in the list. So I tried to check the data type of "search_person" function and it returns "class". I tried to convert it to list by using list() function but also getting error.

Asking for user input

from imdb import Cinemagoer

ia=Cinemagoer()

actors=['Natalie Biggs','Daniel Crossley','Jaye Davidson','Tania Emery','Titus Forbes-Adam','Anthony Houghton','Jane Milligan']

# search for an actor in actors list
people = ia.search_person(input('enter an actor/actress name that exists in the list above: '))
main_person=people[0]
ia.update(main_person)
print(actors.count(main_person))
print(main_person in actors)
Output of asking to user:

Output:
enter an actor/actress name that exists in the list above: Natalie Biggs 0 False Process finished > with exit code 0

If enter the "actor" name in the code like below, the code can find it in the list however I need to ask the user input like above


from imdb import Cinemagoer

ia=Cinemagoer()

actors=['Natalie Biggs','Daniel Crossley','Jaye Davidson','Tania Emery','Titus Forbes-Adam','Anthony Houghton','Jane Milligan']

actor='Natalie Biggs'
print(actors.count(actor))


Output for manually entering the actor name in the code (Successful)

Output:
1 Process finished with exit code 0
Reply
#2
I don't know anything about imdb module but a basic python code wouls look something like

actors=['Natalie Biggs','Daniel Crossley','Jaye Davidson','Tania Emery','Titus Forbes-Adam','Anthony Houghton','Jane Milligan']

# search for an actor in actors list
print(', '.join(actors))
person = input('enter an actor/actress name that exists in the list above:\n>> ')

print(actors.count(person))
print(person)
Output:
Natalie Biggs, Daniel Crossley, Jaye Davidson, Tania Emery, Titus Forbes-Adam, Anthony Houghton, Jane Milligan enter an actor/actress name that exists in the list above: >> Natalie Biggs 1 Natalie Biggs
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
The returned values from search_person is a list with instances of Person.
To get the name, use the square-brackets or the get method on the person instance.
The attribute item with the name is called name.

Counting example:
from imdb import Cinemagoer

first_name = "Arnold"
full_name = "Arnold Schwarzenegger"

query = Cinemagoer()
result = query.search_person(first_name)

names = [actor["name"] for actor in result]
for name in names:
    print(name)

print(full_name, "in results:", names.count(full_name))
You should read the docs: https://cinemagoer.readthedocs.io/en/lat...start.html

Here another example wich uses user input to search for a person and listing all the movies.
from imdb import Cinemagoer


# https://pypi.org/project/IMDbPY/
# IMDbPY is now cinemagoer
# pip install cinemagoer
# https://cinemagoer.github.io/


def ask_one_actor():
    user_input = input(
        f"Should only the first match of an actor listed (y/yes)?: "
    ).lower()
    if user_input in ("y", "yes"):
        return True
    else:
        return False


def main():
    query = Cinemagoer()
    show_only_one_actor = ask_one_actor()
    while True:
        actor = input("Please enter an actor name [q or whitespace to quit]: ").strip()
        # strip() removes whitespaces on left and right side of the str

        # condition to return which leaves the loop
        # a break could also be used, because there are
        # no further instructions are the while True loop

        if not actor.strip() or actor.lower() == "q":
            return

        # the method search_person returns a list
        # with persons (they are not str)

        # the result from query.search_person should be a list
        # index access should be possible
        for actor in query.search_person(actor):

            info_text = f"Query info for actor: {actor}"
            print(info_text)
            print("=" * len(info_text))

            query.update(actor)

            for movie in actor.get_titlesRefs():
                print(movie)

            # two newlines
            print("\n")

            if show_only_one_actor:
                break


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("\nQuit")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020