有與media=null
返回的JSON職位,post['data']['media']
不會有oembed
場(因此,url
場):
{
"kind" : "t3",
"data" : {
"downs" : 24050,
"link_flair_text" : null,
"media" : null,
"url" : "http://youtu.be/aNJgX3qH148?t=4m20s",
"link_flair_css_class" : null,
"id" : "rymif",
"edited" : false,
"num_reports" : null,
"created_utc" : 1333847562,
"banned_by" : null,
"name" : "t3_rymif",
"subreddit" : "videos",
"title" : "An awesome young man",
"author_flair_text" : null,
"is_self" : false,
"author" : "Lostinfrustration",
"media_embed" : {},
"permalink" : "/r/videos/comments/rymif/an_awesome_young_man/",
"author_flair_css_class" : null,
"selftext" : "",
"domain" : "youtu.be",
"num_comments" : 2260,
"likes" : null,
"clicked" : false,
"thumbnail" : "http://a.thumbs.redditmedia.com/xUDtCtRFDRAP5gQr.jpg",
"saved" : false,
"ups" : 32312,
"subreddit_id" : "t5_2qh1e",
"approved_by" : null,
"score" : 8262,
"selftext_html" : null,
"created" : 1333847562,
"hidden" : false,
"over_18" : false
}
},
它也似乎是你的異常信息不會回覆適合:當urlopen
爆炸時會出現很多例外情況,例如IOError
。它不會檢查返回的格式是否爲有效的JSON,因爲您的錯誤消息暗示。
現在,爲了緩解這個問題,你需要檢查是否"oembed" in post['data']['media']
,且僅當它可以調用post['data']['media']['oembed']['url']
,請注意,我做了所有oembed
斑點具有假設url
(主要是因爲你需要一個URL來嵌入在reddit上的媒體)。
**更新: 也就是說,這樣的事情應該解決您的問題:
for post in reddit_posts:
if isinstance(post['data']['media'], dict) \
and "oembed" in post['data']['media'] \
and isinstance(post['data']['media']['oembed'], dict) \
and 'url' in post['data']['media']['oembed']:
print post["data"]["media"]["oembed"]["url"]
reddit_feed.append(post["data"]["media"]["oembed"]["url"])
print reddit_feed
你有一個錯誤的原因是因爲對於一些post
,post["data"]["media"]
爲None
,所以你基本上調用None["oembed"]
這裏。因此錯誤:'NoneType' object is not subscriptable
。我也意識到post['data']['media']['oembed']
可能不是字典,因此如果url
在其中,您還需要驗證它是否是字典和。
更新2:
看起來data
將不存在任何有時,這樣的修補程序:
import json
import urllib
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"))
if isinstance(reddit_posts, dict) and "data" in reddit_posts \
and isinstance(reddit_posts['data'], dict) \
and 'children' in reddit_posts['data']:
reddit_posts = reddit_posts["data"]["children"]
reddit_feed = []
for post in reddit_posts:
if isinstance(post['data']['media'], dict) \
and "oembed" in post['data']['media'] \
and isinstance(post['data']['media']['oembed'], dict) \
and 'url' in post['data']['media']['oembed']:
print post["data"]["media"]["oembed"]["url"]
reddit_feed.append(post["data"]["media"]["oembed"]["url"])
print reddit_feed
你可以做中間打印,然後才能在一次訪問結束對象。例如:'print post ['data']','print post ['data'] ['media']'並找出什麼是返回一個none對象 – karthikr