Python Tweepy – 获取用户 ID
在本文中,我们将了解如何获取用户的 ID。 ID 是 Twitter 帐户的唯一标识标记。 ID是Twitter给用户的,用户不能选择或更改自己的ID。
In order to get the ID we have to do the following :
- Identify the user screen name of the profile.
- Get the User object of the profile using the
get_user()
method and the user name. - From this object, fetch the id or id_str attribute present in it.
示例 1:考虑以下配置文件:
上述个人资料的网名是 geeksforgeeks。
# 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 screen name of the user
screen_name = "geeksforgeeks"
# fetching the user
user = api.get_user(screen_name)
# fetching the ID
ID = user.id_str
print("The ID of the user is : " + ID)
输出 :
The ID of the user is : 57741058
示例 2:考虑以下配置文件:
上述个人资料的网名是 PracticeGfG。
# the screen name of the user
screen_name = "PracticeGfG"
# fetching the user
user = api.get_user(screen_name)
# fetching the ID
ID = user.id_str
print("The ID of the user is : " + ID)
输出 :
The ID of the user is : 4802800777
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。