简体   繁体   English

处理Twitter速率限制

[英]Dealing with Twitter Rate Limit

I have a seemingly simple problem for which I am struggling to find a solution. 我有一个看似简单的问题,正在努力寻找解决方案。 I have a list of ~3,000 tweet ID's for which I wish to get the number of retweets, likes and the number of followers of the user. 我有约3,000条Tweet ID的列表,我希望获得该列表的转发数,点赞数和用户关注者数。

To do this I have written the following code: 为此,我编写了以下代码:

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

However, this will exceed Twitter's rate limits. 但是,这将超过Twitter的速率限制。 How can I introduce a 15 minute wait time when I have reached the rate limit? 达到速率限制后,如何引入15分钟的等待时间?

The tweepy API has a wait_on_rate_limit parameter which is set to False by default. tweepy API有一个wait_on_rate_limit参数,默认情况下将其设置为False

Another example for handling rate limit using cursors is provided in the tweepy docs Code Snippets. 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