Open In App

Python Tweepy – Getting the number of tweets a user has liked

Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will see how we can get number of statuses/tweets a user has liked/favourited. The favourites_count attribute provides us with an integer which denotes the number of statuses the user has favourited.

In order to get the number of statuses the user has favourited, we have to do the following :

  1. Identify the user ID or the screen name of the profile.
  2. Get the User object of the profile using the get_user() method with the user ID or the screen name.
  3. From this object, fetch the favourites_count attribute present in it.

Example 1: Consider the following profile :

We will use the user ID to fetch the user. The user ID of the above mentioned profile is 57741058.




# 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 ID of the user
id = 57741058
  
# fetching the user
user = api.get_user(id)
  
# fetching the favourites_count attribute
favourites_count = user.favourites_count
  
print("The number of tweets the user has liked are : " + str(favourites_count))


Output :

The number of tweets the user has liked are : 483

Example 2: Consider the following profile :

We will use the screen name to fetch the user. The screen name of the above mentioned profile is PracticeGfG.




# the screen name of the user
screen_name = "PracticeGfG"
  
# fetching the user
user = api.get_user(screen_name)
  
# fetching the favourites_count attribute
favourites_count = user.favourites_count
  
print("The number of tweets the user has liked are : " + str(favourites_count))


Output :

The number of tweets the user has liked are : 0


Last Updated : 18 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads