2

Hi I have been trying to remove non existing twitter users from a list. The reason I want to do this is because overtime I run my code some of the twitter users no longer exist and I get this error

kages/tweepy-3.6.0-py3.6.egg/tweepy/binder.py", line 234, in execute
tweepy.error.TweepError: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]

I can't finish collecting my data because this error keeps stopping the code form running so I have been manually going one by one through my list and finding the twitter accounts that don't exist on twitter which is very inefficient. So I have been trying to use a try except block to override this code, as in remove the usernames that no longer exist and keep running my program but everything I have attempted doesn't work. This is my try except block without tweepy (just regular code) which works.

l=["b",23,"c",6,5]
print(l)
for i in range(0,10):
    try:
        for a in l:
            print("a=",int(a))
    except ValueError:
        l.remove(a)
        print(l)
        continue
    else:
        print(l)
        continue

Now I tried to implement this into my twitter list

pro_trump=['TruthSerum888','karlyherron', 'R41nB14ck', 'RedApplePol', 'MKhaoS_86']
for i in range(0,3):
    try:
        for screen_name in pro_trump:
            user_followers=api.friends_ids(id=screen_name)# this returns the interger value of the user id a sepcifc user is following

    except tweepy.error.TweepError:
        print("screen_name that failed=",  screen_name)
        pro_trump.remove(screen_name)
        print(pro_trump)
        continue
    else:# if no error
        print(pro_trump)
        continue

But it doesn't work. it just goes through the pro_trump list removing each username through the list regardless of if the tweeter account still exists or not. Can someone help me implement what I actually wish my code would do?

Bob
  • 279
  • 6
  • 13
  • Can you add the results of the print statements when you the run this code? Also, what is the `for i in range(0,3)` loop for? – Antimony Nov 20 '17 at 02:33
  • the for i loop is to go back through the list so basically the the first time the loop in the try: faces an error it goes to the except and removes the error from the loop so to continue finding more errors (i.e. no longer existing accounts) the for loop continues looping through the pro_trump list. i'll upload some of the output in a short bit. I'm trying to run the code again which as usual twitter is rate limited so i have to wait 15 minutes to do anything new :/ – Bob Nov 20 '17 at 02:46
  • When I tried your code, I noticed that when I got any kind of a Tweepy error, even a rate limit error, it triggered the `except` and caused it to remove IDs. That may be what happened. Otherwise your code works at my end. – Antimony Nov 20 '17 at 02:49

1 Answers1

0

The problem is being caused by your call of api.friends_ids, which throws a rate limit exceeded error. That in turn triggers the except code-block in each loop, regardless of whether the screen-name is right or not. Without the rate limit error, the code works as is. I replaced that call by api.get_user(id=screen_name) to test. You can include an if-else block inside except to only remove users when it's the correct error. I've also optimized your code to place the for loop outside the try-except block.

pro_trump=['TruthSerum888','karlyherron', 'R41nB14ck', 'RedApplePol', 'MKhaoS_86']
for screen_name in pro_trump:
    try:
        user_followers=api.friends_ids(id=screen_name)# this returns the interger value of the user id a sepcifc user is following

    except tweepy.error.TweepError as t:
        if t.message[0]['code'] == 50: # The code corresponding to the user not found error
            print("screen_name that failed=",  screen_name)
            pro_trump.remove(screen_name)
            print(pro_trump)
        elif t.message[0]['code'] == 88: # The code for the rate limit error
            time.sleep(15*60) # Sleep for 15 minutes
    else:# if no error
        print(pro_trump)

This way, you also don't need continues at the end, since it has reached the end of the code-block it was executing, and would continue with the loop anyway.

Antimony
  • 2,230
  • 3
  • 28
  • 38
  • Thank you for the in-depth explanation. I have a couple of questions. I'm confused how the api.friends_ids is the problem since I do the loop every 15 minutes avoiding the rate limiting problem with twitter? Also could you explain what t does and what message does? Thank you for the help! – Bob Nov 20 '17 at 16:44
  • Hmm, I just ran your corrections and It didn't remove the screen name that doesn't exit. this was my output. ['TruthSerum888', 'karlyherron', 'R41nB14ck', 'RedApplePol', 'MKhaoS_86'] ['TruthSerum888', 'karlyherron', 'R41nB14ck', 'RedApplePol', 'MKhaoS_86'] ['TruthSerum888', 'karlyherron', 'R41nB14ck', 'RedApplePol', 'MKhaoS_86'] ['TruthSerum888', 'karlyherron', 'R41nB14ck', 'RedApplePol', 'MKhaoS_86'] ['TruthSerum888', 'karlyherron', 'R41nB14ck', 'RedApplePol', 'MKhaoS_86'] – Bob Nov 20 '17 at 16:49
  • Yes, because all those Twitter IDs exist. I went to each user's page to check. – Antimony Nov 20 '17 at 20:55
  • @Bob Not sure why that method call throws a rate limit error, but I suspect it's something like the issue described here: https://stackoverflow.com/questions/17431807/get-all-follower-ids-in-twitter-by-tweepy `t` is just a variable for the `tweepy.error.TweepError` so that we can see which specific error was thrown, which you can find via `t.message`. – Antimony Nov 20 '17 at 21:32