Open In App

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

The favorites() method of the API class in Tweepy module is used to get the liked statuses of a user, 20 at a time.

Syntax : API.favorites(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 class Status

Example 1 : Using favorites() method without any parameter returns the likes statuses of the authenticated user.




# 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)
  
# getting the liked statuses
favorites = api.favorites()
  
# printing the screen names of the status posters
for status in favorites:
    print(status.user.screen_name)


Output :

oldschoolmonk
notcapnamerica
sardesairajdeep
Artie_Pavlov
BabitaPhogat
notcapnamerica
DerekGreaser
MarkDice
AOC
notcapnamerica
coolfunnytshirt
AOC
CNMparody
marwilliamson
YourAnonCentral
SanjeevSanskrit
4TheCulture____
tify330
AOC
matthewferner

Example 2 : Using favorites() method with the parameter screen name.




# screen name of the user
screen_name = "geeksforgeeks"
  
# getting the liked statuses
favorites = api.favorites(screen_name)
  
# printing the screen names of the status posters
for status in favorites:
    print(status.user.screen_name)


Output :

abhinav26925233
adieti_sharma
vc90
KasatNiraj
harshitabambure
vc90
dns4u
bhagat_dinesh
BafnaTweets
malgattirajat
adieti_sharma
c_deep07
Its_username_yo
Rituraj97082167
Martial_Coder
baniya_buddy_
burgerNpizza
YehTuneKyaKiya
tweetsbybabua
shivankgtm


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