目前我有以下内容解析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'}]
如何将实际网址添加到网址列表中?如果你能解释你的答案中的步骤,那么我可以从中学习,我会很感激。感谢
答案 0 :(得分:1)
这个怎么样。
# your old code avoce
for item in value[art]:
url = urllib2.quote(item['url'], ':/') # Original image url
if url:
image_list.append(url)
答案 1 :(得分:1)
>>> urllist = [{'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'}]
>>> [item['url'] for item in urllist]
['http://fanart.tv/fanart/tv/80349/tvthumb/5287/C_80349%20%284%29.jpg', 'http://fanart.tv/fanart/tv/80349/tvthumb/5286/C_80349%20%285%29.jpg', 'http://fanart.tv/fanart/tv/80349/tvth
umb/5288/C_80349%20%283%29.jpg', 'http://fanart.tv/fanart/tv/80349/tvthumb/5289/C_80349%20%282%29.jpg', u'http://fanart.tv/fanart/tv/80349/tvthumb/5290/C_80349.jpg', 'http://fanart.
tv/fanart/tv/80349/tvthumb/5291/C_80349%20%280%29.jpg']
那应该给你你需要的网址,但看起来像这样:
for item in value[art]:
info = {}
info['url'] = urllib2.quote(item['url'], ':/') # Original image url
if info:
image_list.append(info)
可以简化为:
for item in value[art]:
url = urllib2.quote(item(['url'], ':/')
if url:
image_list.append(url)