'Hit rate limit while fetching replies for a particular tweet

I am using the below code to fetch replies for a tweet with id 1504510003201265684 but I keep getting hit limit warning after a certain amount of replies has been fetched and also it seems some replies are ignored. How can I get around with that?

name = 'netflix'
tweet_id = '1504510003201265684'
count = 0
replies=[]
for tweet in tweepy.Cursor(api.search,q='to:'+name, result_type='mixed',tweet_mode='extended', timeout=999999).items():
    count += 1
    if hasattr(tweet, 'in_reply_to_status_id_str'):
        if (tweet.in_reply_to_status_id_str==tweet_id):
            replies.append(tweet)


Solution 1:[1]

Add a method that handles the tweepy.RateLimitError, in this case it sleeps for 15 minutes

def limit_handled(cursor):
    while True:
        try:
            yield cursor.next()
        except tweepy.RateLimitError:
            time.sleep(15 * 60)

name = 'netflix'
tweet_id = '1504510003201265684'
count = 0
replies=[]
for tweet in limit_handled(tweepy.Cursor(api.search,q='to:'+name, result_type='mixed',tweet_mode='extended', timeout=999999).items()):
    count += 1
    if hasattr(tweet, 'in_reply_to_status_id_str'):
        if (tweet.in_reply_to_status_id_str==tweet_id):
            replies.append(tweet)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tin Nguyen