Open In App

Python – API.trends_available() in Tweepy

Last Updated : 11 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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_available()

The trends_available() method of the API class in Tweepy module is used to fetch the locations that Twitter has trending topic information for.

Syntax : API.trends_available()

Parameters : None

Returns : an object of class JSON

Example 1 :




# 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)
  
# fetching the trends
trends = api.trends_available()
  
print("The number of locations available are : " + str(len(trends)))


Output :

The number of locations available are : 467

Example 2 :




# fetching the trends
trends = api.trends_available()
  
print("Some of the locations are : ")
for i in range(0, 200, 20):
    print("Place : " + trends[i]['name'] +
          ", Country : " + trends[i]['country'])


Output :

Some of the locations are : 
Place : Worldwide, Country : 
Place : Leeds, Country : United Kingdom
Place : Mexico City, Country : Mexico
Place : Mendoza, Country : Argentina
Place : Belo Horizonte, Country : Brazil
Place : Lodz, Country : Poland
Place : Dortmund, Country : Germany
Place : Utrecht, Country : Netherlands
Place : Oslo, Country : Norway
Place : Palembang, Country : Indonesia


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads