简体   繁体   中英

Avoiding rate limit error using tweepy

I am trying to get some basic information on all the twitter friends that a particular user is following. I am using a for loop to get this information but if the user has many friends I get a rate limit error. I am trying and struggling to integrate a way to get around the rate limit into my for loop. Thank you for any suggestions!!

My original code:

data = []
for follower in followers:
    carrot = api.get_user(follower)
    data.append("[")
    data.append(carrot.screen_name)
    data.append(carrot.description)
    data.append("]")

My attempt at getting around rate limit error:

data = []
for follower in followers:
    carrot = api.get_user(follower,include_entities=True)
    while True:
        try:
            data.append("[")
            data.append(carrot.screen_name)
            data.append(carrot.description)
            data.append("]")
        except tweepy.TweepError:
            time.sleep(60 * 15)
            continue
        except StopIteration:
            break

The problem could be it is probably get_user that is throwing the error. Try putting api.get_user in your exception block

Code below.

data = []
for follower in followers:
    while True:
        try:
            carrot = api.get_user(follower,include_entities=True)
        except tweepy.TweepError:
            time.sleep(60 * 15)
            continue
        except StopIteration:
            pass
        break
    data.append([carrot.screen_name, carrot.description])

How do you intend to store these values? Isn't the following easier to work with

[John, Traveller]

as against your code, that stores it as

[ "[", John, Traveller, "]" ]

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