简体   繁体   English

Tweepy错误,响应状态码为429

[英]Tweepy error with response status code 429

I am using tweepy API to pull tweets and display it on the webpage. 我正在使用tweepy API提取推文并将其显示在网页上。 Although I auto-refresh the code to extract tweets every 1 hour, I end up with the following TweepError after about 24 hours of the code continuously running. 尽管我每隔1个小时自动刷新一次代码以提取推文,但在连续运行约24个小时后,我仍然遇到以下TweepError错误。 Error: 错误:

tweepy.error.TweepError: Twitter error response: status code = 429 tweepy.error.TweepError:Twitter错误响应:状态代码= 429

I understand that this code implies that there are too many requests to the API. 我了解此代码表示对该API的请求过多。 Is there any way to handle this error? 有什么办法可以解决这个错误? I have even tried increasing the refresh rate interval to 2 hours, and still doesn't seem to work. 我什至尝试将刷新频率间隔增加到2小时,但似乎仍然不起作用。

Below is the code to extract tweets and store in CSV: 以下是提取推文并以CSV格式存储的代码:

def extract_tweet(self):
   consumer_key = 'XXX'
   consumer_secret = 'XXX'
   access_token = 'YYY'
   access_token_secret = 'YYY'

   auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
   auth.set_access_token(access_token, access_token_secret)

   api = tweepy.API(auth)

   csvFile = open('HELLOTWITTER.csv', 'w',  newline='', encoding='utf-8')
   csvWriter = csv.writer(csvFile)
   for tweet in tweepy.Cursor(api.search,q="#cybersecurity OR #InfoSec", since="2017-10-20",lang="en").items(10):    
       csvWriter.writerow([tweet.text, tweet.created_at])
   csvFile.close()

I have looked at the API documentation and stumbled upon wait_on_rate_limit , but haven't understood if that is the solution to this error. 我看了一下API文档,偶然发现了wait_on_rate_limit ,但是不知道这是否是解决此错误的方法。 Please help! 请帮忙! Thanks. 谢谢。

There are several options to implement retrying decorator. 有几种方法可以实现重试装饰器。 Retry decorator looks if an exception is raised in the method. 重试装饰器查看方法中是否引发异常。 Following example uses exponential backoff retrying. 以下示例使用指数补偿重试。 This will wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards, where x is the number of retry attempt. 每次重试之间将等待2 ^ x * 1000毫秒,最多10秒,然后再等待10秒,其中x是重试次数。

from retrying import retry

@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def extract_tweet(self):
    consumer_key = 'XXX'
    consumer_secret = 'XXX'
    access_token = 'YYY'
    access_token_secret = 'YYY'

    try:
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)

        api = tweepy.API(auth)

        csvFile = open('HELLOTWITTER.csv', 'w',  newline='', encoding='utf-8')
        csvWriter = csv.writer(csvFile)

        for tweet in tweepy.Cursor(api.search,q="#cybersecurity OR #InfoSec", since="2017-10-20",lang="en").items(10):    
          csvWriter.writerow([tweet.text, tweet.created_at])
          csvFile.close()
    except tweepy.error.TweepError:
          raise

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM