Open In App

Python – API.trends_place() in Tweepy

Last Updated : 22 Feb, 2022
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.get_place_trends()

The get_place_trends() method of the API class in Tweepy module is used to fetch the top 50 trending topics for a specific location. 
 

Syntax : API.get_place_trends(id, exclude)
Parameters : 
 

  • id : Yahoo! Where On Earth ID of the location
  • exclude : On setting this parameter to “hashtags”, it excludes hashtags from the list.

Returns : an object of class JSON 
 

Example 1 : 
 

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)
 
# WOEID of London
woeid = 44418
 
# fetching the trends
trends = api.get_place_trends(id = woeid)
 
# printing the information
print("The top trends for the location are :")
 
for value in trends:
    for trend in value['trends']:
        print(trend['name'])


Output : 
 

The top trends for the location are :
Little Britain
#theAword
Peter Shilton
#AHouseThroughTime
Michael O'Neill
#Gclash10
Gladstone
Shilts
White Chicks
Robert Milligan
#GeorgeFloydFuneral
#RhodesMustFall
Prince Philip
Wretch 32
Tower Hamlets
Seyi
Auschwitz
Fact 4
Persona 4 Golden
Matt Lucas
Eddie Murphy
Love Thy Neighbour
Paul Walker
Lord Sainsbury
Fawlty Towers
Peter Hitchens
Chester Zoo
Bogus Journey
Afua
The Hour Of Bewilderbeast
Gareth Southgate
hand of god
#whowantstobeamillionaire
#StupidZoomQuestions
#dailybriefings
#VogueChallenge
#HolbyCity
#SBSwinnershour
#craftbeerhour
#GHB2B
#DontFilterFeelings
#OURfPBookBlether
#GBBO
#landofthemidnightsun
#UnlockingTheNHS
#ShutDownSTEM
#UFC251
#SaveOurZoos
#TWUG
#DisneySongOrBand

Example 2 : Using the parameter exclude. 
 

Python3




# WOEID of New York
woeid = 2459115
 
# fetching the trends
trends = api.get_place_trends(id = woeid, exclude = "hashtags")
 
# printing the information
print("The top trends for the location are :")
 
for value in trends:
    for trend in value['trends']:
        print(trend['name'])


Output : 
 

The top trends for the location are :
Molly
Lakers
Kobe
McDonald
Lawrence
B Simone
Stephen Miller
Henry Winkler
Persona 4 Golden
Mike O'Meara
Kemp
RATM
Stassi
Dick Johnson
Pat Lynch
Chad Daybell
Fulton County
Stan Lee
Neyo
Capitol Hill Autonomous Zone
jax and brittany
Chester Bennington
General Brown
The Batman
James Demarco
Respect is EARNED
Voting Rights Act
Better MC
My 9-5
Ryan Garcia
Denuvo
Congratulations Yamiche

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads