我想开发一个 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”
如果此属性已被删除,那么我应该怎么做才能完成我的工作?请帮忙..
您收到此错误是因为
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']