Python – API.lookup_users() in Tweepy

Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from twitter developer available easily for each user. These keys will help the API for authentication.

API.lookup_users()

The lookup_users() method of the API class in Tweepy module is used to get a list of user objects for up to 100 users per request.

Syntax : API.lookup_users(parameters)

Parameters :

  • user_ids : a list of user IDs
  • screen_names : a list screen names

Returns : a list of objects of the class User



Example 1 :Using the lookup_users() method with user_ids parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import the module
import tweepy
  
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
  
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
# calling the api 
api = tweepy.API(auth)
  
# list of user_ids
user_ids = [57741058, 4802800777, 1037141442]
  
# getting the users by user_ids
users = api.lookup_users(user_ids)
  
# printing the user details
for user in users:
    print("The id is : " + str(user.id))
    print("The screen name is : " + user.screen_name, end = "\n\n")

chevron_right


Output :

The id is : 57741058
The screen name is : geeksforgeeks

The id is : 4802800777
The screen name is : PracticeGfG

The id is : 1037141442
The screen name is : GeeksQuiz

Example 2: Using the lookup_users() method with screen_names parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

# list of screen_names
screen_names = ["geeksforgeeks", "PracticeGfG", "GeeksQuiz"]
  
# getting the users by screen_names
users = api.lookup_users(screen_names = screen_names)
  
# printing the user details
for user in users:
    print("The id is : " + str(user.id))
    print("The screen name is : " + user.screen_name, end = "\n\n")

chevron_right


Output :

The id is : 57741058
The screen name is : geeksforgeeks

The id is : 4802800777
The screen name is : PracticeGfG

The id is : 1037141442
The screen name is : GeeksQuiz

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.