Skip to content
Related Articles

Related Articles

Improve Article
Python – API.lookup_users() in Tweepy
  • Last Updated : 08 Jun, 2020

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.




# 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")

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.




# 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")

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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :