Python:获取tweepy中的Twitter趋势,并解析JSON

Python:获取tweepy中的Twitter趋势,并解析JSON,python,json,twitter,tweepy,Python,Json,Twitter,Tweepy,好的,请注意这是我的第一篇文章 所以,我尝试使用Python来获取Twitter趋势,我使用的是Python 2.7和Tweepy 我想要这样的东西(有效): 这就产生了一个巨大的JSON字符串 我希望将每个趋势名称提取为一个变量,理想情况下为字符串格式str(trendsname) 最理想的趋势名称如下: trendsname=str(趋势1)+“”+str(趋势2)+“” 等等,对于每个趋势名称 请注意,我只是在学习Python。看起来Tweepy为您反序列化了JSON。因此,trends1

好的,请注意这是我的第一篇文章

所以,我尝试使用Python来获取Twitter趋势,我使用的是Python 2.7和Tweepy

我想要这样的东西(有效):

这就产生了一个巨大的JSON字符串

我希望将每个趋势名称提取为一个变量,理想情况下为字符串格式
str(trendsname)

最理想的趋势名称如下:
trendsname=str(趋势1)+“”+str(趋势2)+“”
等等,对于每个趋势名称


请注意,我只是在学习Python。

看起来Tweepy为您反序列化了JSON。因此,
trends1
只是一个普通的Python列表。在这种情况下,您可以简单地执行以下操作:

trends1 = api.trends_place(1) # from the end of your code
# trends1 is a list with only one element in it, which is a 
# dict which we'll put in data.
data = trends1[0] 
# grab the trends
trends = data['trends']
# grab the name from each trend
names = [trend['name'] for trend in trends]
# put all the names together with a ' ' separating them
trendsName = ' '.join(names)
print(trendsName)
结果:

#PolandNeedsWWATour #DownloadElyarFox #DünMürteciBugünHaşhaşi #GalatasaraylılıkNedir #KnowTheTruth Tameni Video Anisa Rahma Mikaeel Manado JDT

我认为以下代码也可以正常工作:

trends1 = api.trends_place(1)
trends = set([trend['name'] for trend in trends1[0]['trends']])
print trends 

您知道内置的
json
模块及其
json.loads()
函数吗?@senshin是的,我知道,但不确定如何使用它。好的,在这种情况下,发布
trends1
的内容。如果它太大而不能放在这里,就把它放在一个空格里。@senshin好的:我相信我想提取“
u'name”:例如,u'#polandneedswawatour',
”值,只要有“
#polandneedswatour
”就可以了,如果这让sensethaks感觉很好的话,我想为每一个趋势的名字提取它!我会投赞成票,但上面说我需要15个名声。忽略以下语句,它是有效的!我可以将trends1设置为JSON字符串的源吗?我假设是这样。@RPiPasswordGen您应该设置
trends1=api.trends\u place(1)
,就像您在问题中所做的那样。这就是你要问的吗?(我刚才把它编辑成了我的答案)
trends1 = api.trends_place(1)
trends = set([trend['name'] for trend in trends1[0]['trends']])
print trends