处理Twitter利率利米

2024-04-27 19:42:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个看似简单的问题,我正在努力寻找解决办法。我有一个约3000推特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分钟的等待时间?你知道吗


Tags: of代码inidfor数量rangechunks
2条回答

tweepy API有一个wait_on_rate_limit参数,默认设置为False。你知道吗

使用游标处理速率限制的另一个示例在tweepy docs代码段中提供。你知道吗

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

相关问题 更多 >