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

The friends() method of the API class in Tweepy module is used to get the specified user’s friends(the users they are following) ordered in which they were added.

Syntax : API.friends(id / user_id / screen_name)

Parameters : Only use one of the 3 options:
id : specifies the ID or the screen name of the user
user_id : specifies the ID of the user, useful to differentiate accounts when a valid user ID is also a valid screen name
screen_name : specifies the screen name of the user, useful to differentiate accounts when a valid screen name is also a user ID
If no user is specified it defaults to the authenticated user.

Returns : a list of objects of the class User



Example 1 :The friends() method returns the 20 most recent friends.

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)
  
# the screen_name of the targeted user
screen_name = "TwitterIndia"
  
# printing the latest 20 friends of the user
for friend in api.friends(screen_name):
    print(friend.screen_name)

chevron_right


Output :

misskaul
rajyasabhatv
DDNewslive
TwitterMedia
abpmajhatv
htTweets
News18Haryana
jack
BBCHindi
the_hindu
mentalhealthind
ProKabaddi
firstpost
livemint
hcmariwala
mathrubhumi
PTUshaOfficial
anubhabhonsle
kmmalleswari
DipaKarmakar

Example 2: More than 20 friends can be accessed using the Cursor() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# the screen_name of the targeted user
screen_name = "TwitterIndia"
  
# getting only 30 friends
for friend in tweepy.Cursor(api.friends, screen_name).items(30):
    print(friend.screen_name)

chevron_right


Output :

misskaul
rajyasabhatv
DDNewslive
TwitterMedia
abpmajhatv
htTweets
News18Haryana
jack
BBCHindi
the_hindu
mentalhealthind
ProKabaddi
firstpost
livemint
hcmariwala
mathrubhumi
PTUshaOfficial
anubhabhonsle
kmmalleswari
DipaKarmakar
NewIndianXpress
M_Raj03
DDNational
isro
PTTVOnlineNews
cricketnext
thebetterindia
AGSawant
MahendraP_BJP
DrRPNishank

Example 3: Counting the number of followers.

filter_none

edit
close

play_arrow

link
brightness_4
code

# the screen_name of the targeted user
screen_name = "geeksforgeeks"
  
# getting all the friends
c = tweepy.Cursor(api.friends, screen_name)
  
# counting the number of friends
count = 0
for friends in c.items():
    count += 1
print(screen_name + " has " + str(count) + " friends.")

chevron_right


Output :

geeksforgeeks has 8 friends.



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.