简体   繁体   中英

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. I've been reading about the Twitter rate limit but it still isn't quite making sense. The documentation says "15 calls every 15 minutes, and 180 calls every 15 minutes." However, when I run my code without the sleep function I manage to get around 280 names before twitter cuts me off. 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. 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. Just increase the count to 5000. The call uses followers/list call. Here is the link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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