Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rate limit reached. Sleeping for:

Tags:

python

tweepy

i'm collecting tweets withe thier replies from Twitter's API to build data set and i'm using tweepy library in python for that,but the problem is that I get this error so much (Rate limit reached. Sleeping for:(any number for sec)) that delays me and I have to collect as many data as possible in the shortest time

I read that twitter has a rate limit of i think 15 requests per 15 minutes or something like that, but on my situation I can only gather a tweet or two tweet until it stops again and sometimes it stops for 15 minutes and then stop again for 15 minutes without giving me give me time between them, I don't know what caused the problem whether it is my code or not?

# Import the necessary package to process data in JSON format
try:
    import json
except ImportError:
    import simplejson as json

# Import the tweepy library
import tweepy
import sys

# Variables that contains the user credentials to access Twitter API 
ACCESS_TOKEN = '-'
ACCESS_SECRET = '-'
CONSUMER_KEY = '-'
CONSUMER_SECRET = '-'

# Setup tweepy to authenticate with Twitter credentials:

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

# Create the api to connect to twitter with your creadentials
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)



file2 = open('replies.csv','w', encoding='utf-8-sig') 

replies=[]   
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)  
for full_tweets in tweepy.Cursor(api.search,q='#عربي',timeout=999999,tweet_mode='extended').items():
    if (not full_tweets.retweeted) and ('RT @' not in full_tweets.full_text):
        for tweet in tweepy.Cursor(api.search,q='to:'+full_tweets.user.screen_name,result_type='recent',timeout=999999,tweet_mode='extended').items(1000):
            if hasattr(tweet, 'in_reply_to_status_id_str'):
                if (tweet.in_reply_to_status_id_str==full_tweets.id_str):
                    replies.append(tweet.full_text)
        print(full_tweets._json)
        file2.write("{ 'id' : "+ full_tweets.id_str + "," +"'Replies' : ")  
        for elements in replies:
                file2.write(elements.strip('\n')+" , ")      
        file2.write("}\n")
        replies.clear()
 
    
 
file2.close()

$ python code.py > file.csv

Rate limit reached. Sleeping for: 262 

Rate limit reached. Sleeping for: 853
like image 376
s99 Avatar asked Sep 05 '25 14:09

s99


1 Answers

Hoping this might help

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=False, compression=True)
like image 74
Venkatakrishnan R Avatar answered Sep 10 '25 09:09

Venkatakrishnan R