Can someone please tell me how to get one username from one id on tweepy? I have looked everywhere and can't find anything.
If you just have a user_id value you need to call the twitter API with the get_user(user_id) method . The returned User object will contain the username under screen_name.
# steps not shown where you set up api
u = api.get_user(783214)
print u.screen_name
If you already have the User object from another API call just look for the screen_name.
You can use this code to get user screen name or user id
To get user screen name from user id
In [36]: import tweepy
In [37]: consumer_key = Your_consumerkey
In [38]: consumer_secret = Your_consuersecret
In [39]: access_token = Your_access_token
In [40]: access_token_secret = Your_access_token_secret
In [41]: auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
In [42]: auth.set_access_token(access_token, access_token_secret)
In [43]: api = tweepy.API(auth)
In [48]: user = api.get_user(1088398616)
In [49]: user.screen_name
Out[49]: u'saimadhup'
To get user id from user screen name
In [46]: user = api.get_user(screen_name = 'saimadhup')
In [47]: user.id
Out[47]: 1088398616
Although OP clearly needs the username for just one id, in case if one wants to get usernames for a list of ids(<100), then:
def get_usernames(ids):
""" can only do lookup in steps of 100;
so 'ids' should be a list of 100 ids
"""
user_objs = api.lookup_users(user_ids=ids)
for user in user_objs:
print(user.screen_name)
For larger set of ids, you can just put this in a for
loop and call accordingly while obeying the twitter API limit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.