I am really new to twitter api, and I’ve been trying to get a list of IDs of everyone that retweeted a specific tweet.
After several attempts i can’t get the ‘api.get_retweeter_ids’ to get every id. It always seems to get a few. I know there is a limit of 100 per request, but the function just ends there after getting around 50-90 IDs on a tweet with 30k retweets or so.
Here is my code
def get_user_ids_by_retweets(tweetid):
retweeters_ids = []
for i, _id in enumerate(tweepy.Cursor(api.get_retweeter_ids, id=tweetid).items()):
retweeters_ids.append(_id)
print(i, _id)
df = pd.DataFrame(retweeters_ids)
# print(df)
return retweeters_ids
2
Answers
Demo for get all of re tweeter user list (name, id and username)
code
Result
Main Idea
Get tweeter API return limited number of tweeters with
next_token
.It can be next paging’s tweeter by assign to
pagination_token
.It can be all of tweeter until ‘next_token` is null.
So #1 and #2 get two tweeters with
next_token
, those sum tweeters are same as #3 tweeters.References
List of objects to JSON with Python
Python – Append to JSON File
Twitter API v2 Retweet
I would avoid managing the tokens manually, if not needed. The
Paginator
is the tool for it (it’s the API V2 version of the API V1.1Cursor
that you’ve tried to use). If you are sure that the amount of retweets is covered by the currently available number of requests (default is 100 retweeters per request) then you could try the following (it’s the equivalent to the other answer):If you’re not sure about it but just want to give it a try without loosing any retrieved retweeters, then you could use this variation which catches the resp.
tweepy.errors.TooManyRequests
exception:If you want to make sure that you get all retweeters, then you could add a waiting period that is tailored to your access level (if you’re using the free version then you should have 75 requests per 15 minutes, i.e. after reaching the limit you need to wait 60 * 15 seconds). Here you need to use the token to re-enter at the point where you left in case the rate limit was reached: