I am very new to Python and I have a json news feed that I need to get a selected ‘title’ and image ‘src’.
I have managed to get to print all the ‘title’ and just the image ‘src’ that says “1024 landscape”.
How can I print, for example, just the second title? How do I address that particular one?
The feed is : http://www.stuff.co.nz/_json/ipad-big-picture
for story in data.get('stories', []):
print 'Title:', story['title']
for img in story.get('images', []):
for var in img.get('variants', []):
if var.get('layout') == "1024 Landscape":
print ' img:', (var.get('src')).split('/')[-1], ' layout:', var.get('layout')
Thanks
First just get your stories object (list of dicts):
Once you have this list you can just access by index:
Or try first and catch the exception:
So, when trying to get all 1024 Landscape images for a specific story, it might look like this: