【问题标题】:Dealing with Twitter Rate Limit处理 Twitter 速率限制
【发布时间】:2018-12-17 05:12:46
【问题描述】:

我有一个看似简单的问题,我正在努力寻找解决方案。我有一个包含约 3,000 个推文 ID 的列表,我希望从中获取用户的转推数、点赞数和关注者数。

为此,我编写了以下代码:

def chunks(l, n):
    # For item i in a range that is a length of l,
    for i in range(0, len(l), n):
        # Create an index range for l of n items:
        yield l[i:i+n]

tweets = []
id = list(chunks(listOfTwitterIDs, 100))
for each in id:
    tweets.append(api.statuses_lookup(each, map=true))

但是,这将超出 Twitter 的速率限制。当我达到速率限制时,如何引入 15 分钟的等待时间?

【问题讨论】:

  • time.sleep(15 * 60)

标签: python twitter tweepy rate-limiting


【解决方案1】:

tweepy API 有一个wait_on_rate_limit 参数,默认设置为False

tweepy docs 代码片段中提供了另一个使用游标处理速率限制的示例。

【讨论】:

    【解决方案2】:
    def chunks(l, n):
        # For item i in a range that is a length of l,
        for i in range(0, len(l), n):
            # Create an index range for l of n items:
            yield l[i:i+n]
    
    tweets = []
    id = list(chunks(listOfTwitterIDs, 100))
    for each in id:
        # try to get get # of retweets
        try:
            tweets.append(api.statuses_lookup(each, map=true))
        # If it fails, wait 15 mins and 1 sec (just to be safe) and try again
        except: 
            sleep(901)
            tweets.append(api.statuses_lookup(each, map=true))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2011-11-16
      • 2015-07-09
      • 2012-01-20
      • 1970-01-01
      相关资源
      最近更新 更多