简体   繁体   中英

Search by user.name on Tweepy

I have a list of people names and want to find out if they have Twitter accounts, unreliable as that may be. How can I do it on Tweepy?

I am trying to pass user.name as an argument for the query, but it just seems to allow for searching their screen_names or ids.

user = api.get_user(screen_name='georgetakei')
print(user.name)
**> George Takei**

But if I try the other way round...

user = api.get_user(name='George Takei')
print(user.screen_name)
**> TweepError: [{'code': 50, 'message': 'User not found.'}]**

Is there a workaround?

Could you use the api.search_users() method?

Example using a list

#Search for users by name
name = 'George Takei'
users = api.search_users(q=name)

#Print the screen_name of the first matching user
if len(users) > 0:
    print(users[0].screen_name)
else:
    print("No user found with that name.")

Not 100% sure, but I think api.search_users(q, count) should work as well. As in q being query and count being the number of users to be retrieved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM