如何使用Python和tweepy获取Twitter趋势?

问题描述 投票:0回答:2

我想开发一个 Jupyter Notebook,每次执行时都会显示过去 24 小时内印度排名前 10 的 Twitter 热门话题。

我已经把一切都设置好了:

auth = tweepy.OAuthHandler(apikey,apisecretkey)
auth.set_access_token(accesskey,accesssecret)
api = tweepy.API(auth)

当我运行

trends1 = api.trends_place(23424848)
时,它给出:

AttributeError:“API”对象没有属性“trends_place”

如果此属性已被删除,那么我应该怎么做才能完成我的工作?请帮忙..

python twitter jupyter-notebook
2个回答
1
投票

您收到此错误是因为

api
没有此
trends_place
属性。 如果你检查文档(https://docs.tweepy.org/en/stable/api.html#trends),你会发现正确的语法是
api.trends_place()
,而不是使用
api.
,后跟属性
get_place_trends()
.

因此,我建议使用以下代码来获得所需的结果:

auth = tweepy.OAuthHandler(apikey,apisecretkey)
auth.set_access_token(accesskey,accesssecret)
api = tweepy.API(auth)

WOEID = 23424848

top_trends = api.get_place_trends(WOEID)

请注意,

top_trends
是长度为1的
list
内的字典。将其视为
top_trends[0]['trends']
等以获取特定值。示例:

top_trends[0]['trends'][0]['name']
top_trends[0]['trends'][0]['url']
top_trends[0]['trends'][0]['promoted_content']
top_trends[0]['trends'][0]['query']
top_trends[0]['trends'][0]['tweet_volume']

0
投票

要获取 Twitter 上特定位置附近的热门话题

设置完成后:

auth = tweepy.OAuthHandler(apikey,apisecretkey)
auth.set_access_token(accesskey,accesssecret)
api = tweepy.API(auth)

使用

trends = api.trends_place(WOEID)
根据lWOEID

获取50个热门话题
© www.soinside.com 2019 - 2024. All rights reserved.