简体   繁体   中英

tweepy.TweepError Rate Limit Exceeded

Question

I was writing a Twitter bot with Python an Tweepy. I successfully got the bot to work last night, however, after following 60 people, the bot started throwing an error that says [{u'message': u'Rate Limit Exceeded', u'code': 88}] . I understand that I am only allowed to do a certain amount of calls to the Twitter API and I have found this link that shows how many calls I can make on each of these functions. After reviewing my code, I have found that the error is being thrown where I am saying for follower in tweepy.Cursor(api.followers, me).items(): . On the page that I found that says how many requests I get, it says that I get 15 requests for every 15 minutes for getting my followers. I have waited overnight and I retried the code this morning, however, it was still throwing the same error. I don't understand why Tweepy is throwing a rate limit exceeded error whenever I should still have requests left.

Code

Here is my code that's throwing the error.

#!/usr/bin/python

import tweepy, time, pprint

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
me = api.me()

pprint.pprint(api.rate_limit_status())

while True:
    try:
        for follower in tweepy.Cursor(api.followers, me).items():
            api.create_friendship(id=follower.id)
        for follower in tweepy.Cursor(api.friends, me).items():
            for friend in tweepy.Cursor(api.friends, follower.id).items():
                if friend.name != me.name:
                    api.create_friendship(id=friend.id)
    except tweepy.TweepError, e:
        print "TweepError raised, ignoring and continuing."
        print e
        continue

I have found the line that throws the error by typing in the interactive prompt

for follower in tweepy.Cursor(api.followers, me).items():
        print follower

where it gives me the error

**Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    for follower in api.followers(id=me.id):
  File "C:\Users\Lane\AppData\Local\Enthought\Canopy\User\lib\site-packages\tweepy\binder.py", line 239, in _call
    return method.execute()
  File "C:\Users\Lane\AppData\Local\Enthought\Canopy\User\lib\site-packages\tweepy\binder.py", line 223, in execute
    raise TweepError(error_msg, resp)
TweepError: [{u'message': u'Rate limit exceeded', u'code': 88}]**

API().followers method is actually GET followers/list , which is limited to 15 requests per window (before next epoch). Each call returns 20 users list by default , and if you are reaching limit exceed error, I'm sure authenticated user has more than 300 followers.

The solution is to increase the length of user's list received during each call, such that within 15 calls it can fetch all the users. Modify your code as following

for follower in tweepy.Cursor(api.followers, id = me.id, count = 50).items():
    print follower

count denotes the number of users to be fetched per request.

Maximum value of count can be 200, it means within 15 minute, you can only fetch 200 x 15 = 3000 followers, which is enough in general scenarios.

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