I am trying to parse this JSON from the tvmaze api. The JSON that is returned has one object as None. This is causing the for loop to break. How can I catch this error and skip it?
The simple code looks like this:
import requests,re,json
url = "http://api.tvmaze.com/shows/1/seasons"
html = requests.get(url).json()
for season in html:
images = season['image']
test = images['medium']
print test
This results in this error:
Traceback (most recent call last):
File "C:/Python27/test_maze.py", line 7, in <module>
if 'medium' not in images:
TypeError: argument of type 'NoneType' is not iterable
I can see that if I print imagesthe result is:
{u'medium': u'http://static.tvmaze.com/uploads/images/medium_portrait/24/60941.jpg', u'original': u'http://static.tvmaze.com/uploads/images/original_untouched/24/60941.jpg'}
{u'medium': u'http://static.tvmaze.com/uploads/images/medium_portrait/24/60942.jpg', u'original': u'http://static.tvmaze.com/uploads/images/original_untouched/24/60942.jpg'}
None
I have tried multiple versions of if 'medium' not in images, but I get this error:
TypeError: argument of type 'NoneType' is not iterable
if 'medium' not in images:line is not present in the code you show.