2012-10-15 24 views
1

當我運行下面的代碼時,在瀏覽器中顯示TypeError。錯誤出現在最後一行,並說'NoneType'對象不是可以下載的(我正在嘗試獲取所有項目的所有URL)。然而,這很奇怪,因爲在命令行中,Feed中的所有URL都會打印出來。關於爲什麼這些項目在命令行中打印但在瀏覽器中顯示錯誤的任何想法?我該如何解決?如何解決與解析JSON有關的Django中的TypeError?

#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 

編輯

if post["data"]["media"]["oembed"]["url"]: 
    print post["data"]["media"]["oembed"]["url"] 
+0

你可以做中間打印,然後才能在一次訪問結束對象。例如:'print post ['data']','print post ['data'] ['media']'並找出什麼是返回一個none對象 – karthikr

回答

2

有與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 

你有一個錯誤的原因是因爲對於一些postpost["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 
+0

我試過上面的問題中包含的編輯,但它似乎沒有工作,因爲我得到相同的錯誤。有關我如何實際執行它的任何建議? – sharataka

+0

@sharataka看到我更新的答案。 –

+0

我收到更新代碼後,仍然得到相同的錯誤('NoneType'類型的參數不可迭代),它指向帶有「oembed」的新linke。這可能是我如何定義reddit_feed的問題嗎? json.loads(f.read()。decode(「utf-8」))[「data」] [「children」],特別是[「data」]和[「children」]? – sharataka