【发布时间】:2019-08-13 10:47:15
【问题描述】:
我正在使用 Tweepy 从 Twitter Streaming API 下载推文。我设法检查下载的数据是否具有“extended_tweet”这样的键,但我正在努力处理另一个键中的特定键。
def on_data(self, data):
savingTweet = {}
if not "retweeted_status" in data:
dataJson = json.loads(data)
if 'extended_tweet' in dataJson:
savingTweet['text'] = dataJson['extended_tweet']['full_text']
else:
savingTweet['text'] = dataJson['text']
if 'coordinates' in dataJson:
if 'coordinates' in dataJson['coordinates']:
savingTweet['coordinates'] = dataJson['coordinates']['coordinates']
else:
savingTweet['coordinates'] = 'null'
我正在正确地检查 'extended_key',但是当我尝试对 ['coordinates]['coordinates] 执行相同操作时,我收到以下错误:
TypeError: argument of type 'NoneType' is not iterable
Twitter 文档称关键“坐标”具有以下结构:
"coordinates":
{
"coordinates":
[
-75.14310264,
40.05701649
],
"type":"Point"
}
我通过尝试尝试冲突检查来解决它,但我认为这不是解决问题的最合适的方法。还有什么想法吗?
【问题讨论】:
标签: python json twitter key tweepy