Python – API.followers() in Tweepy
Last Updated :
05 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.followers()
The followers()
method of the API
class in Tweepy module is used to get the specified user’s followers ordered in which they were added.
Syntax : API.followers(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
Returns : a list of objects of the class User
Example 1 :The followers() method returns the 20 most recent followers.
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
screen_name = "geeksforgeeks"
for follower in api.followers(screen_name):
print (follower.screen_name)
|
Output :
ManojChevula
davidasem_
rajneesosho
sl_jens
Sandeep06081
abdulwhabalras2
chryptografio
_Mohit_Rajput_
elamineyamani
abhiSharma0311
PawanYa87321156
yagneshmb
yogeekabhi
DarioGallardo2
imharsan
MOHAMMA18757894
ArunKum79855492
ans_human_ap
1nonlyabhi
Example 2: More than 20 followers can be accessed using the Cursor()
method.
screen_name = "geeksforgeeks"
for follower in tweepy.Cursor(api.followers, screen_name).items( 30 ):
print (follower.screen_name)
|
Output :
mr_manav852
MohsinI09002010
educatorly
JimXu_artist
JaiHin303
Indrajeet307
sainisajal147
aryangarg22
Ashutos11691851
ManojChevula
davidasem_
osho957
sl_jens
Sandeep06081
abdulwhabalras2
chryptografio
_Mohit_Rajput_
elamineyamani
abhiSharma0311
PawanYa87321156
yagneshmb
yogeekabhi
DarioGallardo2
imharsan
MOHAMMA18757894
ArunKum79855492
ans_human_ap
1nonlyabhi
SimplicitySol2
prateekmaj21
Example 3: Counting the number of followers.
screen_name = "geeksforgeeks"
c = tweepy.Cursor(api.followers, screen_name)
count = 0
for follower in c.items():
count + = 1
print (screen_name + " has " + str (count) + " followers." )
|
Output :
geeksforgeeks has 17338 followers.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...