Python json get specific story and image -
i new python , have json news feed need selected 'title' , image 'src'.
i have managed print 'title' , image 'src' says "1024 landscape". how can print, example, second title? how address particular one? feed : http://www.stuff.co.nz/_json/ipad-big-picture
for story in data.get('stories', []): print 'title:', story['title'] img in story.get('images', []): var in img.get('variants', []): if var.get('layout') == "1024 landscape": print ' img:', (var.get('src')).split('/')[-1], ' layout:', var.get('layout')
thanks
first stories object (list of dicts):
stories = data.get('stories', [])
once have list can access index:
if len(stories) >= 2: print stories[1]['title']
or try first , catch exception:
i = 1 try: print stories[i]['title'] except indexerror: print "story not exist @ index %d" %
so, when trying 1024 landscape images specific story, might this:
imgs = set() img in stories[1].get('images', []): variant in img.get('variants', []): if variant.get('layout') == '1024 landscape': imgs.add(variant['src']) print imgs set([u'http://static.stuff.co.nz/1341147692/827/7202827.jpg'])
Comments
Post a Comment