Python “接收”;TypeError:JSON对象必须是str、bytes或bytearray,而不是dict";
我有一个Tweepy的json输出,我现在正试图解析它。例如,一些输出是特定区域的趋势标签。因为它是一个大的输出,所以我试图确定如何有效地解析所有的hashtag。json输出中还有其他信息,如Python “接收”;TypeError:JSON对象必须是str、bytes或bytearray,而不是dict";,python,json,tweepy,Python,Json,Tweepy,我有一个Tweepy的json输出,我现在正试图解析它。例如,一些输出是特定区域的趋势标签。因为它是一个大的输出,所以我试图确定如何有效地解析所有的hashtag。json输出中还有其他信息,如userid,countrycode,等等。。。但我只对列为name:'#gamenight的hashtag感兴趣 # using Tweepy api.trends_place(2295420) import json # Here is a portion of the Tweepy outp
userid
,countrycode
,等等。。。但我只对列为name:'#gamenight
的hashtag感兴趣
# using Tweepy
api.trends_place(2295420)
import json
# Here is a portion of the Tweepy output I received
trends = [{'trends': [{'name': '#RCBvKKR', 'url': 'http://twitter.com/search?q=%23RCBvKKR', 'promoted_content': None, 'query': '%23RCBvKKR', 'tweet_volume': 101508}, {'name': 'created_at': '2019-04-06T00:07:14Z', 'locations': [{'name': 'Bangalore', 'woeid': 2295420}]}]
hashtags = json.dumps(trends)
# Am trying to end up with a way of just extracting 'name' which I believe is how the hashtags are captured
print(hashtags['name'])
接收
#RCBvKKR
好的,我修好了。首先,发布的代码令人困惑。您发布的json无效(缺少括号,名称键没有值)。其次,使用命令
json.dumps(trends)
,您正在将已经有效的python字典转换为字符串,这是一个数组,因此会出现错误(字符串索引必须是整数)
固定版本如下所示:
import json
trends = [{'trends': [{'name': '#RCBvKKR', 'url': 'http://twitter.com/search?q=%23RCBvKKR', 'promoted_content': None, 'query': '%23RCBvKKR', 'tweet_volume': 101508}, {'name':"This was missing", 'created_at': '2019-04-06T00:07:14Z', 'locations': [{'name': 'Bangalore', 'woeid': 2295420}]}]}]
print(trends[0]["trends"][0]["name"])
现在输出为#RCBvKKR
如果确实从API接收到json字符串,请使用json.parse(response)
将字符串转换为python dict。我收到另一个错误类型错误:字符串索引必须为integers@Tweep,是的,我修复了它。我想我把收到的Tweepy输出错误地标记为JSON,而不知道它是不是。我仍在尝试确定api.trends\u place的Tweepy输出的格式。i、 它是数组、哈希、json吗?我还试图检索“name”的每个实例,而不仅仅是“RCBvKRR”