apt45
apt45

Reputation: 433

Get number of retweeters with tweetpy

I use tweepy to do some twitter analysis. I wanted to look at the list of users which retweets a given tweet. First of all, I want to extract the number of retweeters of this tweet using tweepy.

I use the following code

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

count=0
for tweet in api.retweets(1090392302130888704):
     countj+=1

print(countj)

As you can see from the link, the number of retweets is 54. However, this code returns 50. Why is there this discrepancy?

I have tried to appy this code to several tweets and I notice there is always a discrepancy from what I see with the Web Client and the result of the code.

Upvotes: 1

Views: 799

Answers (1)

Harmon758
Harmon758

Reputation: 5157

Protected Retweets are shown as part of the count you see, but you're unable to obtain them or their Retweeters through the API (unless that protected account follows you).

To outline this, you can see that https://twitter.com/AmericaTalks/status/1090408203882360832 has 7 Retweets right now. If you click to see who Retweeted, it'll show 6 accounts, and at the bottom, it'll say "1 user has asked not to be shown in this view. Learn More". The API will also return only the 6 Retweet(er)s.

Note, in your code, you define count, but use countj. This will result in a NameError.
Also, API.retweets returns a list of Status objects, so you can just do len(api.retweets(1090392302130888704)), instead of looping through them to count them.

Upvotes: 2

Related Questions