Thursday, September 4, 2025
HomeLanguagesPython – API.lookup_friendships() in Tweepy

Python – API.lookup_friendships() 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_friendships()

The lookup_friendships() method of the API class in Tweepy module is used to get the detailed relationship between the authenticated user and the list of users, upto 100 at a time.

Syntax : API.lookup_friendships(user_ids / screen_names)

Parameters : Only use one of the 2 options:

  • user_ids : a list that specifies the IDs of the users.
  • screen_names : a list that specifies the screen names of the users.

Returns : an object of the class Relationship

Example 1 : Analyzing the relationships by using the screen names.




# 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 screen names
screen_names = ["SrBachchan",
                "akshaykumar",
                "imVkohli",
                "sachin_rt",
                "SonuSood"]
  
# getting the friendship details
friendships = api.lookup_friendships(screen_names = screen_names)
  
for friendship in friendships:
    print("Is the authenticated user following " + friendship.screen_name, end = "? : ")
    print(friendship.is_following)


Output :

Is the authenticated user following SrBachchan? : False
Is the authenticated user following akshaykumar? : True
Is the authenticated user following imVkohli? : True
Is the authenticated user following sachin_rt? : False
Is the authenticated user following SonuSood? : False

Example 2 : Analyzing the relationships by using the user IDs.




# list of user IDs
user_ids = [813286,
            27260086,
            21447363,
            79293791,
            17919972]
  
# getting the friendship details
friendships = api.lookup_friendships(user_ids = user_ids)
  
for friendship in friendships:
    print("Is the authenticated user following " + friendship.screen_name, end = "? : ")
    print(friendship.is_following)


Output :

Is the authenticated user following BarackObama? : True
Is the authenticated user following justinbieber? : False
Is the authenticated user following katyperry? : False
Is the authenticated user following rihanna? : False
Is the authenticated user following taylorswift13? : False
Last Updated :
08 Jun, 2020
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS