Wednesday, July 3, 2024
HomeLanguagesPythonPython Tweepy – Getting the language of a tweet

Python Tweepy – Getting the language of a tweet

In this article we will see how we can fetch the language of a tweet/status. The lang attribute of the Status object indicates the language of the status. The language is indicated in the language code example, English is en, Hindi is hi.

In order to get the language of the status, we have to do the following :

  1. Identify the status ID of the status from the GUI.
  2. Get the Status object of the status using the get_status() method with the status ID.
  3. From this object, fetch the lang attribute present in it.

Example 1 : Consider the following status :

We will use the status ID to fetch the status. The status ID of the above mentioned status is 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)


Output :

The language of the status is : en

Example 2 : Consider the following status :

We will use the status ID to fetch the status. The status ID of the above mentioned status is 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)


Output :

The language of the status is : hi

Last Updated :
21 Jun, 2020
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments