I need to obtain all the followers of a Twitter account that has aprox 125K follores. So I run this code:
import tweepy
auth = tweepy.OAuth2AppHandler(api_key, api_secret)
api = tweepy.API(auth)
tweepy.Cursor(api.get_followers,screen_name=sN,count=100).items(125000)
Credentials are under a Development App on an Elevated Developer Account.
And I got this error:
TooManyRequests: 429 Too Many Requests 88 - Rate limit exceeded
Is there a paginator I can uset to request lest items and obtain the 125000 followers? How can I complement this code with Cursor pages?
Thanks!
On 04/22/2023 I run this:
auth = tweepy.OAuth1UserHandler(
trikini.api_key, trikini.api_secret
)
api = tweepy.API(auth, wait_on_rate_limit=True)
first_net = []
for status in tweepy.Cursor(api.get_followers, screen_name=sN,
count=200).items():
print(status.id)
first_net.append(status.id
#status.screen_name]
)
And got this error: Unauthorized: 401 Unauthorized Not authorized.
Then I tried this:
import tweepy
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret,
access_token, access_token_secret
)
api = tweepy.API(auth, wait_on_rate_limit=True)
first_net = []
for status in tweepy.Cursor(api.get_followers, screen_name=sN,
count = 200).items(125000):
print(status.screen_name)
ids.append([status.id,status.screen_name])
with open(r'filename.txt', 'w') as fp:
for item in ids:
fp.write("%s\n" % item)
first_net
The code ended its execution, but I just got 252 IDs, and the user masked with sN had 112565 followers. What may had happened?
The error
TooManyRequests: 429 Too Many Requests 88 - Rate limit exceededis being thrown, because you exceeded the standard rate limit.Check out the Twitter API rate limits, which are the same for tweepy.
Standard rate limits:
There is a parameter (wait_on_rate_limit) that you can use, which will mitigate the error. This parameter will put your query session into a sleep mode once you hit the rate limit threshold. The parameter is designed to put up the session once the rate limit threshold has restarted.
Here is how it is used. The reference below is from the code base.
Here is another example reference from the
tweepy.APIand the code below is from that reference:UPDATED 04.24.2023
After doing more research into this question, I found that
tweepyhas a bug in the code base that doesn't maintain the state of a session when using the parameterwait_on_rate_limitwith either Twitter's API v1.1 or v2.0In API v1.1 and API v2.0 the bug is in the function
requestin this code. The bug in API v2.0 is linked torequests.sessions.There is an open tweepy issue on this bug.
Both the code examples below for me got 1000s of users before the rate limit threshold was triggered.
Here is the code that I used for API v1.1:
Here is the code that I used for API v2.0:
Here is some useful information on handling disconnections with the Twitter API.