简体   繁体   English

了解Twitter的速率限制

[英]Understanding the Twitter rate limit

I'm using Tweepy to write a function that will return all the followers for a large Twitter account and write them to a file. 我正在使用Tweepy编写一个函数,该函数将返回大型Twitter帐户的所有关注者并将其写入文件。 I've been reading about the Twitter rate limit but it still isn't quite making sense. 我一直在阅读有关Twitter速率限制的信息,但仍然不太合理。 The documentation says "15 calls every 15 minutes, and 180 calls every 15 minutes." 该文档说:“每15分钟打15个电话,每15分钟打180个电话”。 However, when I run my code without the sleep function I manage to get around 280 names before twitter cuts me off. 但是,当我运行没有睡眠功能的代码时,在Twitter将我拒之门外之前,我设法获得了大约280个名称。 So how many calls am I actually making here? 那么我在这里实际上打了多少个电话? My code is as follows: 我的代码如下:

import tweepy
import time

auth = tweepy.OAuthHandler("...", "...")
auth.set_access_token("...", "...")
api = tweepy.API(auth)

f = open('output.txt', 'w')
timecount = 0
for user in tweepy.Cursor(api.followers, screen_name="NAME").items():
    timecount = timecount + 1
    if timecount == 200:    
        print "HOLD ON A SECOND!!!"
        #print api.rate_limit_status()
        time.sleep(60*15)
        timecount = 0   
    data = user.screen_name 
    print user.screen_name  
    print >> f, data   
f.close()

Right now it's waiting 15 minutes between every 280 names it gets which seems to be working. 目前,它在每280个名称之间等待15分钟,这似乎正在起作用。 Obviously, I want this to run as efficiently as possible. 显然,我希望它能够尽可能高效地运行。 Can anyone help me understand how many calls I'm making how long I should be waiting? 谁能帮助我了解我正在打多少个电话,我应该等待多长时间?

在这种情况下,数学非常简单,实际上您在Twitter将您截断之前发出了14个请求,但是您能够获取280个名称,因为tweepy.Cursor(api.followers, screen_name="NAME")是一个返回的单个请求一次20个值,这意味着您在单个请求中获取20个值,并且正如您提到的那样,您能够获取280个名称,这并不奇怪,因为280/20 = 14因此,实际上您只发出了14个请求,您只需遍历280个值即可打印出名称等。有关更多详细信息,请参阅文档

I would add this in comments but I cannot due reputation and stuffs. 我会在评论中添加此内容,但我无法享有声誉和东西。 I had the same issue a while ago. 前段时间我遇到了同样的问题。 You can actually fetch up to 5000 usernames per request meaning that you can get 75000 followers every 15 min. 实际上,每个请求最多可以获取5000个用户名,这意味着每15分钟可以获得75000个关注者。 Just increase the count to 5000. The call uses followers/list call. 只需将计数增加到5000即可。该呼叫使用关注者/列表呼叫。 Here is the link . 这是链接

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

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