检索Twitter趋势时出现TwitterHTTPError

2024-04-28 21:37:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我在努力理解为什么我会犯这个错误。这段代码来自我正在上的在线课程。我正在将本地的灾难ID更改为本地的灾难ID,这是我得到错误的时候。如果我用大城市的身份证,似乎没问题。在

import pickle
import os
if not os.path.exists('secret_twitter_credentials.pkl'):
    Twitter={}
    Twitter['Consumer Key'] = '...'
    Twitter['Consumer Secret'] = '...'
    Twitter['Access Token'] = '...'
    Twitter['Access Token Secret'] = '...'
    with open('secret_twitter_credentials.pkl','wb') as f:
        pickle.dump(Twitter, f)
else:
    Twitter=pickle.load(open('secret_twitter_credentials.pkl','rb'))

WORLD_WOE_ID = 1
US_WOE_ID = 23424977

LOCAL_WOE_ID=2344925

# Prefix ID with the underscore for query string parameterization.
# Without the underscore, the twitter package appends the ID value
# to the URL itself as a special case keyword argument.

world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)
local_trends = twitter_api.trends.place(_id=LOCAL_WOE_ID)

我得到这个错误。在

^{pr2}$

Tags: theapiidsecret错误placetwitterpickle
2条回答

我上的是同一门课,也犯了同样的错误。在

我找到了一个解决办法。既然雅虎不再发布WOEID,而你只需要在Twitter应用程序中使用它,那么你就可以通过这种方式找到最近的上市城市;运行

twitter_api.trends.available() 

在你的jupyter笔记本中(当然是在安装和导入twitter之后)。这将为您提供一个城市列表,您可以ctrl+f。在

问题似乎是由于2344925的LOCAL_WOE_ID对Twitter API无效。在

Twitter提供了另一个API twitter.trends.available(),它提供了所有支持的可用woeid的列表。API文档位于:https://developer.twitter.com/en/docs/trends/locations-with-trending-topics/api-reference/get-trends-available.html

2344925没有出现在这个列表中(尽管它在其他一些WOEID查找中也可以找到,例如https://www.flickr.com/places/info/2344925),这可能是由于Yahoo!目前不完全支持他们的“地球上的何处”数据(https://en.wikipedia.org/wiki/WOEID)。在

相关问题 更多 >