已达到速率限制。睡觉的目的:

s99*_*s99 5 python tweepy

我正在收集推文以及来自 Twitter 的 API 的回复来构建数据集,并且我正在 python 中使用 tweepy 库来实现这一点,但问题是我经常收到此错误(已达到速率限制。正在睡觉) :(秒的任何数字))这会耽误我,我必须在最短的时间内收集尽可能多的数据

\n

我读到 Twitter 的速率限制是每 15 分钟 15 个请求或类似的东西,但在我的情况下,我只能收集一两条推文,直到它再次停止,有时它会停止 15 分钟,然后再次停止15分钟不给我给我时间,我不知道是什么导致了问题,是否是我的代码?

\n
# Import the necessary package to process data in JSON format\ntry:\n    import json\nexcept ImportError:\n    import simplejson as json\n\n# Import the tweepy library\nimport tweepy\nimport sys\n\n# Variables that contains the user credentials to access Twitter API \nACCESS_TOKEN = \'-\'\nACCESS_SECRET = \'-\'\nCONSUMER_KEY = \'-\'\nCONSUMER_SECRET = \'-\'\n\n# Setup tweepy to authenticate with Twitter credentials:\n\nauth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)\nauth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)\n\n# Create the api to connect to twitter with your creadentials\napi = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)\n\n\n\nfile2 = open(\'replies.csv\',\'w\', encoding=\'utf-8-sig\') \n\nreplies=[]   \nnon_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)  \nfor full_tweets in tweepy.Cursor(api.search,q=\'#\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a\',timeout=999999,tweet_mode=\'extended\').items():\n    if (not full_tweets.retweeted) and (\'RT @\' not in full_tweets.full_text):\n        for tweet in tweepy.Cursor(api.search,q=\'to:\'+full_tweets.user.screen_name,result_type=\'recent\',timeout=999999,tweet_mode=\'extended\').items(1000):\n            if hasattr(tweet, \'in_reply_to_status_id_str\'):\n                if (tweet.in_reply_to_status_id_str==full_tweets.id_str):\n                    replies.append(tweet.full_text)\n        print(full_tweets._json)\n        file2.write("{ \'id\' : "+ full_tweets.id_str + "," +"\'Replies\' : ")  \n        for elements in replies:\n                file2.write(elements.strip(\'\\n\')+" , ")      \n        file2.write("}\\n")\n        replies.clear()\n \n    \n \nfile2.close()\n
Run Code Online (Sandbox Code Playgroud)\n

$ python code.py > file.csv

\n
Rate limit reached. Sleeping for: 262 \n\nRate limit reached. Sleeping for: 853\n
Run Code Online (Sandbox Code Playgroud)\n

小智 -4

只需将此行添加到 Python 脚本即可避免睡眠:

sleep_on_rate_limit=False
Run Code Online (Sandbox Code Playgroud)