I get a TypeError displaying in the browser when I run the code below. The error comes at the last line and says ‘NoneType’ object is not subscriptable (I am trying to get all the urls for all the items). However it is odd because in the command line, all the urls in the feed get printed. Any ideas on why the items are getting printed in the command line but showing an error in the browser? How do I fix this?
#reddit parse
try:
f = urllib.urlopen("http://www.reddit.com/r/videos/top/.json");
except Exception:
print("ERROR: malformed JSON response from reddit.com")
reddit_posts = json.loads(f.read().decode("utf-8"))["data"]["children"]
reddit_feed=[]
for post in reddit_posts:
if "oembed" in post['data']['media']:
print post["data"]["media"]["oembed"]["url"]
reddit_feed.append(post["data"]["media"]["oembed"]["url"])
print reddit_feed
edit
if post["data"]["media"]["oembed"]["url"]:
print post["data"]["media"]["oembed"]["url"]
There are posts in the returned json with
media=nullsopost['data']['media']will not haveoembedfield (and hence,urlfield):It also seems to be that your exception message doesn’t really fit: there are many kinds of exceptions that can be thrown when
urlopenblows up, such asIOError. It does not check on whether the returned format is valid JSON as your error message imply.Now, to mitigate the problem, you need to check if
"oembed" in post['data']['media'], and only if it does can you callpost['data']['media']['oembed']['url'], notice that I am making the assumption that alloembedblob hasurl(mainly because you need an URL to embed a media on reddit).**UPDATE:
Namely, something like this should fix your problem:
The reason you have that error is because for some
post,post["data"]["media"]isNoneand so you are basically callingNone["oembed"]here. And hence the error:'NoneType' object is not subscriptable. I’ve also realized thatpost['data']['media']['oembed']may not be a dict and hence you will also need to verify if it is a dict and ifurlis in it.Update 2:
It looks like
datawon’t exist sometimes either, so the fix: