Python Tweepy – Getting the number of times a tweet has been retweeted

In this article we will see how we can get the number of times a tweet/status has been retweeted. The retweet_count attribute of the Status object provides us with the number of times a tweet has been retweeted .

Identifying the number of times a tweet has been retweeted in the GUI :

The above mentioned status has been retweeted 4 times.

In order to get the number of times a status has been retweeted, we have to do the following :

  1. Identify the status ID of the status from the GUI.
  2. Get the Status object of the status using the get_status() method with the status ID.
  3. From this object, fetch the retweet_count attribute present in it.

Example 1 : Consider the following status :



We will use the status ID to fetch the status. The status ID of the above mentioned status is 1272771459249844224.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 status
id = 1272771459249844224
  
# fetching the status
status = api.get_status(id)
  
# fetching the retweet_count attribute
retweet_count = status.retweet_count 
  
print("The number of time the status has been retweeted is : " + str(retweet_count))

chevron_right


Output :

The number of time the status has been retweeted is : 0

Example 2 : Consider the following status :

We will use the status ID to fetch the status. The status ID of the above mentioned status is 1272479136133627905.

filter_none

edit
close

play_arrow

link
brightness_4
code

# the ID of the status
id = 1272479136133627905
  
# fetching the status with extended tweet_mode
status = api.get_status(id, tweet_mode = "extended")
  
# fetching the retweet_count attribute
retweet_count = status.retweet_count 
  
print("The number of time the status has been retweeted is : " + str(retweet_count))

chevron_right


Output :

The number of time the status has been retweeted is : 22

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.