简体   繁体   中英

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.

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. How can I introduce a 15 minute wait time when I have reached the rate limit?

The tweepy API has a wait_on_rate_limit parameter which is set to False by default.

Another example for handling rate limit using cursors is provided in the tweepy docs Code Snippets.

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

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