Tuesday, 30 March 2021

Handling rate limit exceptions with Cursor in tweepy

I'm trying to find the correct way to handle rate limits when iterating through a list of followers using the Cursor object. Here is what I'm trying:

while True:
    try:
        for follower in tweepy.Cursor(api.followers, id=root_usr).items():
            print(follower.id)
    except tweepy.TweepError:
        # hit rate limit, sleep for 15 minutes
        print('Rate limited. Sleeping for 15 minutes.')
        time.sleep(15 * 60 + 15)
        continue
    except StopIteration:
        break

This is probably incorrect, since an exception will make the for loop start from the beginning again. What's the right way to iterate through all of root_usr's followers while also handling the rate limit problems?



from Handling rate limit exceptions with Cursor in tweepy

No comments:

Post a Comment