KieranDL3
KieranDL3

Reputation: 83

Get list of user id's from a users Twitter followers using Tweepy

I have written the following code to get a list of all the Twitter followers a username has but I would like to just get the user_id instead as this allows me to do a lot more within the rate limit for the API. How do I change this code to just print the user_ids?

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key, secret)
api = tweepy.API(auth)

screen_name = "twitterusername"
  
friends = api.friends_ids(screen_name) 
  
print(screen_name + " is following :") 
for friend in friends: 
    print(api.get_user(friend).screen_name)

I have all of the correct authorisation keys and tokens.

Upvotes: 1

Views: 1183

Answers (1)

Beppe C
Beppe C

Reputation: 13983

The User object has an id field

print(screen_name + " is following :") 
for friend in friends: 
  print(api.get_user(friend).id)

If you are interested in the IDs only of the users be aware that api.friends_ids returns already a list of IDs

friends = api.friends_ids('screen_name')
logging.info(friends)
# output: [324343434, 134343234567,...]

Upvotes: 1

Related Questions