OneManRiot
OneManRiot

Reputation: 971

Using Tweepy to turn a list of screen names into user IDs

I'm writing a basic program using Python and Tweepy to take a list of Twitter screen names and pull down the corresponding user IDs. I've got the rate limiter implemented and the program works but things fall apart when it hits my exception handling. It's telling me that the screen name in X isn't present after it waits the 15 minutes. I need exception handling as Tweepy often runs into issues while running. What am I doing wrong here?

f = open('output2.txt', 'w')
while True:
  for x in HandleList1:
    try:    
        u = api.get_user(id = x)
        print >> f, u.id
    except tweepy.TweepError:
        print "We just hit an error, waiting for 15min and then reconnecting..."
        time.sleep(60*15)
        u = api.get_user(id = x)
        print >> f, u.id
    except StopIteration:
        print "Stopping the iteration and processing the results!"
        break

f.close()

Upvotes: 1

Views: 235

Answers (1)

Johannes Wachs
Johannes Wachs

Reputation: 1310

I guess that TweepError covers multiple kinds of errors, including rate-limit errors and query errors. If you are searching for a username that no longer exists you may get the same error.

Check out how to print the exact kind of error you are running into here: Get the error code from tweepy exception instance

I would add an if-else statement to your except tweepy.TweepError catch that checks if the error is a ratelimit error or something else as explained in the link. In the latter case you can just pass (or prints the error and the specific query you made).

Upvotes: 1

Related Questions