从python中的json键获取相应的值 [英] Get corresponding value from json key in python

查看:826
本文介绍了从python中的json键获取相应的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下解析json api的文件.

currently I have the following which parses a json api..

import simplejson
import urllib2

f = urllib2.urlopen('http://fanart.tv/webservice/series/f5b8e776c35e7536eac132802b03281f/80349/json/tvthumb/')  
data = simplejson.load(f)

imagetypes = ['clearlogo', 'clearart', 'tvthumb', 'seasonthumb', 'characterart']

image_list = []
# split "name" and "data"
for title, value in data.iteritems():
# run through specified types
for art in imagetypes:
# if type has been found
    if value.has_key(art):
    # Run through all the items
        for item in value[art]:
            info = {}
            info['url'] = urllib2.quote(item['url'], ':/') # Original image url
            if info:
                image_list.append(info)



print image_list

这将返回以下内容.

[{'url': 'http://fanart.tv/fanart/tv/80349/tvthumb/5287/C_80349%20%284%29.jpg'},
{'url': 'http://fanart.tv/fanart/tv/80349/tvthumb/5286/C_80349%20%285%29.jpg'},
{'url': 'http://fanart.tv/fanart/tv/80349/tvthumb/5288/C_80349%20%283%29.jpg'},
{'url': 'http://fanart.tv/fanart/tv/80349/tvthumb/5289/C_80349%20%282%29.jpg'},
{'url': u'http://fanart.tv/fanart/tv/80349/tvthumb/5290/C_80349.jpg'}, {'url':
'http://fanart.tv/fanart/tv/80349/tvthumb/5291/C_80349%20%280%29.jpg'}]

如何将仅实际的URL放入URL列表中?如果您能解释答案中的步骤,以便我可以从中学习,我将不胜感激.谢谢

How do I go about getting just the actual url into a list of urls? If you could explain the steps in your answer so I can learn from it I'd appreciate it. Thanks

推荐答案

如何?

    # your old code avoce
    for item in value[art]:
        url = urllib2.quote(item['url'], ':/') # Original image url
        if url:
            image_list.append(url)

这篇关于从python中的json键获取相应的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆