Python – API.trends_place() in Tweepy
Last Updated :
22 Feb, 2022
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 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)
woeid = 44418
trends = api.get_place_trends( id = woeid)
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 = 2459115
trends = api.get_place_trends( id = woeid, exclude = "hashtags" )
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
Share your thoughts in the comments
Please Login to comment...