Skip to content
Related Articles

Related Articles

Improve Article

Python – API.trends_place() in Tweepy

  • Last Updated : 11 Jun, 2020

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

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

Syntax : API.trends_place(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 :




# 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.trends_place(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.




# WOEID of New York
woeid = 2459115
  
# fetching the trends
trends = api.trends_place(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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :