Skip to content
Related Articles

Related Articles

Improve Article
Python – API.list_subscribers() in Tweepy
  • Last Updated : 10 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.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.




# 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 sceen 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.




# the slug of the list
slug = "thought-leaders"
  
# the sceen 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 subscriers fetched : " + str(len(subscribers)))

Output :

The number of subscriers fetched : 3

 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 :