繁体   English   中英

处理Twitter速率限制

[英]Dealing with Twitter Rate Limit

我有一个看似简单的问题,正在努力寻找解决方案。 我有约3,000条Tweet 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分钟的等待时间?

tweepy API有一个wait_on_rate_limit参数,默认情况下将其设置为False

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

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))

暂无
暂无

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

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