Python Tweepy – Getting the screen name of a user
Last Updated :
03 Jul, 2020
In this article we will see how we can get the screen name of a user. Screen name is the username of the twitter account. It is the name that users choose to identify themselves on the network. Many users choose to either use their real name as the basis for their screen name, often in a shortened version. All the screen names must be unique.
Identifying the screen name in the GUI :
In the above mentioned profile, geeksforgeeks is the screen name of the profile.
In order to get the screen name we have to do the following :
- Identify the user ID of the profile.
- Get the User object of the profile using the
get_user()
method and the user ID.
- From this object, fetch the screen_name attribute present in it.
Example 1: Consider the following profile :
The user ID of the above mentioned profile is 57741058.
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
id = 57741058
user = api.get_user( id )
screen_name = user.screen_name
print ( "The screen name of the user is : " + screen_name)
|
Output :
The screen name of the user is : geeksforgeeks
Example 2: Consider the following profile :
The user ID of the above mentioned profile is 4802800777.
id = 4802800777
user = api.get_user( id )
screen_name = user.screen_name
print ( "The screen name of the user is : " + screen_name)
|
Output :
The screen name of the user is : PracticeGfG
Share your thoughts in the comments
Please Login to comment...