Open In App

Python – API.list_subscribers() in Tweepy

Improve
Improve
Like Article
Like
Save
Share
Report

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.list_subscribers()

The list_subscribers() method of the API class in Tweepy module is used fetch the subscribers of a specified list. 

Syntax : API.list_subscribers(parameters)
Parameters : 
 

  • list_id : ID of the list.
  • slug : slug of the list.
  • owner_id : ID of the owner of the list.
  • owner_screen_name : screen name of the owner of the list.
  • count : number of subscribers to be fetched.
  • skip_statuses : boolean determining whether to return statuses or not.

Returns : a list of objects of class User 
 

Example 1 : Printing the screen names of the 20 latest subscribers. 

Python3




# 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 slug of the list
slug = "thought-leaders"
 
# the screen name of the owner of the list
owner_screen_name = "kitson"
 
# fetching the subscribers
subscribers = api.list_subscribers(slug = slug,
                                   owner_screen_name = owner_screen_name)
 
# printing the screen names of the subscribers
for subscriber in subscribers:
    print(subscriber.screen_name)


Output :  

Saddds305
rachellelive
hazemkhattab8
constantine_fry
rockivy4
edwinealmonte
satishvkvn
puppies221122
55_CancriF
jameschancepro1
LiptakCarl
TheMarkShaw
mitch_plzzzz
ahrimango
nidhishetty
jmpena33
TuttleRose
rgolwalkar
BillRingle
Nellie_I_Am

Example 2 : Using the count parameter to fetch only a specified number of subscribers. 

Python3




# the slug of the list
slug = "thought-leaders"
 
# the screen name of the owner of the list
owner_screen_name = "kitson"
 
# number of subscribers to be fetched
count = 3
 
# fetching the subscribers
subscribers = api.list_subscribers(slug = slug,
                                   owner_screen_name = owner_screen_name,
                                   count = count)
 
print("The number of subscribers fetched : " + str(len(subscribers)))


Output : 

The number of subscribers fetched : 3


Last Updated : 21 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads