Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

parsing - Get corresponding value from json key in python

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

This returns the following..

[{'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'}]

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about this.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...