Python Tweepy – 获取推文的语言
在本文中,我们将了解如何获取推文/状态的语言。 Status 对象的 lang 属性表示状态的语言。语言代码示例中指明了语言,英语为 en,印地语为 hi。
In order to get the language of the status, we have to do the following :
- Identify the status ID of the status from the GUI.
- Get the Status object of the status using the
get_status()
method with the status ID. - From this object, fetch the lang attribute present in it.
示例 1:考虑以下状态:
我们将使用状态 ID 来获取状态。上述状态的状态 ID 为 1272771459249844224。
# 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 status
id = 1272771459249844224
# fetching the status
status = api.get_status(id)
# fetching the lang attribute
lang = status.lang
print("The language of the status is : " + lang)
输出 :
The language of the status is : en
示例 2:考虑以下状态:
我们将使用状态 ID 来获取状态。上述状态的状态 ID 为 1273220141417816064。
# the ID of the status
id = 1273220141417816064
# fetching the status
status = api.get_status(id)
# fetching the lang attribute
lang = status.lang
print("The language of the status is : " + lang)
输出 :
The language of the status is : hi
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。