Tweepy Look up ID with username
Question:
I am trying to get a list of IDs from a list of usernames I have. Is there any method that tweepy provides that lets me do lookup user IDs using their username?
Answers:
Twitter API has the resource https://dev.twitter.com/rest/reference/get/users/lookup for such requirements. It can return user objects for at most 100 users at a time.
You can use this in Tweepy like:
user_objects = api.lookup_users(screen_names=list_of_at_most_100_screen_names)
user_ids = [user.id_str for user in user_objects]
For anyone who landed here from Google, this is a code snippet that is basically a username to id converter. It supports twitter api V2.
# the usernames variable is a list containing all the usernames you want to convert
usernames = ["POTUS", "VP"]
users = client.get_users(usernames=usernames)
for user in users.data:
print(user.id)
screen_name = unames['username']
enter code here
#my username df
#0 briankrebs
#1 Dejan_Kosutic
#2 msftsecresponse
#3 PrivacyProf
#4 runasand
data = []
def return_twitterid(screen_name):
print("The screen name is: " + screen_name)
twitterid = client.get_user(username=screen_name)
id = twitterid.data.id
return id
for s in range(len(screen_name)):
u_id = return_twitterid(screen_name[s])
data.append(u_id)
print(data)
I am trying to get a list of IDs from a list of usernames I have. Is there any method that tweepy provides that lets me do lookup user IDs using their username?
Twitter API has the resource https://dev.twitter.com/rest/reference/get/users/lookup for such requirements. It can return user objects for at most 100 users at a time.
You can use this in Tweepy like:
user_objects = api.lookup_users(screen_names=list_of_at_most_100_screen_names)
user_ids = [user.id_str for user in user_objects]
For anyone who landed here from Google, this is a code snippet that is basically a username to id converter. It supports twitter api V2.
# the usernames variable is a list containing all the usernames you want to convert
usernames = ["POTUS", "VP"]
users = client.get_users(usernames=usernames)
for user in users.data:
print(user.id)
screen_name = unames['username']
enter code here
#my username df
#0 briankrebs
#1 Dejan_Kosutic
#2 msftsecresponse
#3 PrivacyProf
#4 runasand
data = []
def return_twitterid(screen_name):
print("The screen name is: " + screen_name)
twitterid = client.get_user(username=screen_name)
id = twitterid.data.id
return id
for s in range(len(screen_name)):
u_id = return_twitterid(screen_name[s])
data.append(u_id)
print(data)