Python – API.trends_closest() in Tweepy
Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from twitter developer available easily for each user. These keys will help the API for authentication.
API.trends_closest()
The trends_closest()
method of the API
class in Tweepy module is used to fetches the locations that Twitter has trending topic information for.
Syntax : API.trends_closest(lat, long)
Parameters :
- lat : latitude of the location.
- long : longitude of the location.
Returns : an object of class JSON
Example 1 : Using the trends_closest()
method for Delhi.
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
lat = 28
long = 77
locations = api.trends_closest(lat, long )
print ( str ( len (locations)) + " location(s) is / are fetched." )
print ( "\nThe location(s) is / are :" )
for location in locations:
print (location[ 'name' ])
|
Output :
1 location(s) is/are fetched.
The location(s) is/are :
Delhi
Example 2 : Exception is raised when giving invalid coordinates.
lat = 200
long = 114
locations = api.trends_closest(lat, long )
|
Output :
raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'code': 3, 'message': 'Invalid coordinates.'}]
Last Updated :
11 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...