0

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
Obsidian
  • 515
  • 3
  • 10
Noele
  • 1
  • 2

1 Answers1

0

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, "]" ]

Obsidian
  • 515
  • 3
  • 10