Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:34:14+00:00 2026-06-12T23:34:14+00:00

I get a TypeError displaying in the browser when I run the code below.

  • 0

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"]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:34:15+00:00Added an answer on June 12, 2026 at 11:34 pm

    There are posts in the returned json with media=null so post['data']['media'] will not have oembed field (and hence, url field):

         {
            "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
            }
         },
    

    It also seems to be that your exception message doesn’t really fit: there are many kinds of exceptions that can be thrown when urlopen blows up, such as IOError. 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 call post['data']['media']['oembed']['url'], notice that I am making the assumption that all oembed blob has url (mainly because you need an URL to embed a media on reddit).

    **UPDATE:
    Namely, something like this should fix your problem:

    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
    

    The reason you have that error is because for some post, post["data"]["media"] is None and so you are basically calling None["oembed"] here. And hence the error: 'NoneType' object is not subscriptable. I’ve also realized that post['data']['media']['oembed'] may not be a dict and hence you will also need to verify if it is a dict and if url is in it.

    Update 2:

    It looks like data won’t exist sometimes either, so the fix:

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error: TypeError: object.__init__() takes no parameters when running my code, I
this is the error i get: Uncaught TypeError: Object #<HTMLDivElement> has no method this
start_url=requests.get('http://www.delicious.com/golisoda') soup=BeautifulSoup(start_url) this code is displaying the following error: Traceback (most recent call last):
I get TypeError: iterable argument required when i try to execute the code below:
I get TypeError: Result of expression 'localStorage' [null] is not an object when I
According to should.js Spec this should work: should.strictEqual(shape.code, code) but I get: TypeError: Object
I get this error TypeError at /debate/1/ get_context_data() takes exactly 2 arguments (1 given)
I get an incomplete type error when trying to compile my code. I know
If I call QApplication's init without arguments i get TypeError: arguments did not match
What's wrong with this code? I get: TypeError: older is undefined (10 out of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.