Python Tweepy – 获取用户的位置
在本文中,我们将了解如何获取用户的位置。用户帐户的位置不必是用户的确切物理位置。由于用户可以自由更改他们的位置,因此帐户的位置甚至可以通过假设的位置。 location 属性是可选的并且可以为 Nullable。
在 GUI 中识别位置:
在上面提到的配置文件中,印度是配置文件的位置。
In order to get the location we have to do the following :
- Identify the user ID or the screen name of the profile.
- Get the User object of the profile using the
get_user()
method with the user ID or the screen name. - From this object, fetch the location attribute present in it.
示例 1:考虑以下配置文件:
我们将使用用户 ID 来获取用户。上述配置文件的用户 ID 是 57741058。
Python3
# 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 location
location = user.location
print("The location of the user is : " + location)
Python3
# the screen name of the user
screen_name = "PracticeGfG"
# fetching the user
user = api.get_user(screen_name)
# fetching the name
name = user.name
if location == "":
print("The user has not mentioned their location.")
else:
print("The location of the user is : " + location)
输出 :
The location of the user is : India
示例 2:考虑以下配置文件:
我们将使用屏幕名称来获取用户。上述个人资料的网名是 PracticeGfG。这里没有提到位置。
Python3
# the screen name of the user
screen_name = "PracticeGfG"
# fetching the user
user = api.get_user(screen_name)
# fetching the name
name = user.name
if location == "":
print("The user has not mentioned their location.")
else:
print("The location of the user is : " + location)
输出 :
The user has not mentioned their location.
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。