Python Tweepy - 获取用户的屏幕名称
在本文中,我们将了解如何获取用户的屏幕名称。屏幕名称是 Twitter 帐户的用户名。它是用户选择在网络上标识自己的名称。许多用户选择使用他们的真实姓名作为他们的屏幕名称的基础,通常是缩短版本。所有屏幕名称必须是唯一的。
在 GUI 中识别屏幕名称:
在上面提到的配置文件中,geeksforgeeks 是配置文件的网名。
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.
示例 1:考虑以下配置文件:
上述配置文件的用户 ID 是 57741058。
# import the module
import tweepy
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
# the ID of the user
id = 57741058
# fetching the user
user = api.get_user(id)
# fetching the screen name
screen_name = user.screen_name
print("The screen name of the user is : " + screen_name)
输出 :
The screen name of the user is : geeksforgeeks
示例 2:考虑以下配置文件:
上述配置文件的用户 ID 是 4802800777。
# the ID of the user
id = 4802800777
# fetching the user
user = api.get_user(id)
# fetching the screen name
screen_name = user.screen_name
print("The screen name of the user is : " + screen_name)
输出 :
The screen name of the user is : PracticeGfG
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。