当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Tweepy API.trends_place()用法及代码示例


Twitter是一种流行的社交网络,用户在其中共享称为推文的消息。 Twitter允许我们使用Twitter API或Tweepy挖掘任何用户的数据。数据将是从用户提取的推文。首先要做的是从每个用户都可以轻松获得Twitter开发人员的使用者 key ,使用者 key ,访问 key 和访问 key 。这些 key 将帮助API进行身份验证。

API.trends_place()

的trends_place()方法APITweepy模块中的类用于获取特定位置的前50个热门主题。

用法:API.trends_place(id, exclude)

参数:

  • id:雅虎!该位置的地球ID
  • exclude:将此参数设置为“hashtags”时,它将从列表中排除井号。

返回值:JSON类的对象



范例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'])

输出:

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

范例2:使用参数排除。

# 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'])

输出:

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



相关用法


注:本文由纯净天空筛选整理自Yash_R大神的英文原创作品 Python – API.trends_place() in Tweepy。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。