urllib2 and exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robean

    urllib2 and exceptions

    Hi everyone,

    I have a question about using urllib2.

    I like urllib2 better than urllib at least in part because it has more
    elaborate support for handling errors: there is built in support for
    URLError (for faulty urls) and HTTPError (for http errors that might
    originate from, say, passing an invalid stock-ticker in the program
    below). However I can get neither to work. I'm attaching below the
    (very short) code: can anyone point out what I'm doing wrong?

    Now, if I replace the URLError and HTTPError with IOError (the class
    from which both URLError and HTTPError inherit), the program works
    fine. Why is it that I can call the generic IOError class, but none of
    the Error classes derived from that? These are clearly defined in the
    urllib2 manual. Very confused...

    Here's the code:


    import urllib2

    # read stock information from yahoo finance for Traget (TGT)
    goodTicker = 'TGT' # program works with this
    badTicker = 'TGTttttttt' # python doesn't understand either HTTPError
    or URLError with this

    url = "http://ichart.finance. yahoo.com/table.csv?s=" + badTicker

    try:
    handle = urllib2.urlopen (url)

    # this does not work
    except HTTPError, e:
    print "There was an http error"
    print e

    # this also does not work
    except URLError, e:
    print "There is a problem with the URL"
    print e
    exit(1)

    #this works
    except IOError, e:
    print "You have an IOError"
    print e

    text = handle.readline s()[:20]
    for line in text:
    print line

  • Chris Rebert

    #2
    Re: urllib2 and exceptions

    On Sun, Sep 28, 2008 at 11:03 AM, robean <st1999@gmail.c omwrote:
    Hi everyone,
    >
    I have a question about using urllib2.
    >
    I like urllib2 better than urllib at least in part because it has more
    elaborate support for handling errors: there is built in support for
    URLError (for faulty urls) and HTTPError (for http errors that might
    originate from, say, passing an invalid stock-ticker in the program
    below). However I can get neither to work. I'm attaching below the
    (very short) code: can anyone point out what I'm doing wrong?
    >
    Now, if I replace the URLError and HTTPError with IOError (the class
    from which both URLError and HTTPError inherit), the program works
    fine. Why is it that I can call the generic IOError class, but none of
    the Error classes derived from that? These are clearly defined in the
    urllib2 manual. Very confused...
    >
    Here's the code:
    >
    >
    import urllib2
    >
    # read stock information from yahoo finance for Traget (TGT)
    goodTicker = 'TGT' # program works with this
    badTicker = 'TGTttttttt' # python doesn't understand either HTTPError
    or URLError with this
    >
    url = "http://ichart.finance. yahoo.com/table.csv?s=" + badTicker
    >
    try:
    handle = urllib2.urlopen (url)
    >
    # this does not work
    except HTTPError, e:
    print "There was an http error"
    print e
    >
    # this also does not work
    except URLError, e:
    print "There is a problem with the URL"
    print e
    exit(1)
    >
    #this works
    except IOError, e:
    print "You have an IOError"
    print e
    >
    text = handle.readline s()[:20]
    for line in text:
    print line
    >
    --

    >
    My Python begs to differ:

    #tmp.py
    import urllib2

    badTicker = 'TGTttttttt'
    url = "http://ichart.finance. yahoo.com/table.csv?s=" + badTicker

    try:
    handle = urllib2.urlopen (url)

    except urllib2.HTTPErr or, e:
    print "There was an http error"
    print e

    except urllib2.URLErro r, e:
    print "There is a problem with the URL"
    print e

    except urllib2.IOError , e:
    print "You have an IOError"
    print e

    #in the shell
    $ python -V
    Python 2.5.1
    $ python Desktop/tmp.py
    There was an http error
    HTTP Error 404: Not Found

    Are you using an outdated version of Python perhaps?

    Regards,
    Chris

    --
    Follow the path of the Iguana...

    Comment

    • robean

      #3
      Re: urllib2 and exceptions

      On Sep 28, 12:11 pm, "Chris Rebert" <c...@rebertia. comwrote:
      On Sun, Sep 28, 2008 at 11:03 AM, robean <st1...@gmail.c omwrote:
      Hi everyone,
      >
      I have a question about using urllib2.
      >
      I like urllib2 better than urllib at least in part because it has more
      elaborate support for handling errors: there is built in support for
      URLError (for faulty urls) and HTTPError (for http errors that might
      originate from, say, passing an invalid stock-ticker in the program
      below).  However I can get neither to work.  I'm attaching below the
      (very short) code: can anyone point out what I'm doing wrong?
      >
      Now, if I replace the URLError and HTTPError with IOError (the class
      from which both URLError and HTTPError inherit), the program works
      fine. Why is it that I can call the generic IOError class, but none of
      the Error classes derived from that? These are clearly defined in the
      urllib2 manual. Very confused...
      >
      Here's the code:
      >
      import urllib2
      >
      # read stock information from yahoo finance for Traget (TGT)
      goodTicker = 'TGT' # program works with this
      badTicker = 'TGTttttttt' # python doesn't understand either HTTPError
      or URLError with this
      >
      url = "http://ichart.finance. yahoo.com/table.csv?s=" + badTicker
      >
      try:
             handle = urllib2.urlopen (url)
      >
      # this does not work
      except HTTPError, e:
             print "There was an http error"
             print e
      >
      # this also does not work
      except URLError, e:
             print "There is a problem with the URL"
             print e
             exit(1)
      >
      #this works
      except IOError, e:
             print "You have an IOError"
             print e
      >
      text = handle.readline s()[:20]
      for line in text:
             print line
      >>
      My Python begs to differ:
      >
      #tmp.py
      import urllib2
      >
      badTicker = 'TGTttttttt'
      url = "http://ichart.finance. yahoo.com/table.csv?s=" + badTicker
      >
      try:
          handle = urllib2.urlopen (url)
      >
      except urllib2.HTTPErr or, e:
          print "There was an http error"
          print e
      >
      except urllib2.URLErro r, e:
          print "There is a problem with the URL"
          print e
      >
      except urllib2.IOError , e:
          print "You have an IOError"
          print e
      >
      #in the shell
      $ python -V
      Python 2.5.1
      $ python Desktop/tmp.py
      There was an http error
      HTTP Error 404: Not Found
      >
      Are you using an outdated version of Python perhaps?
      >
      Regards,
      Chris
      >
      --
      Follow the path of the Iguana...http://rebertia.com
      Then I expect that it is most likely my version of python that is
      causing the problem. I'm using 2.5.2.

      Comment

      • robean

        #4
        Re: urllib2 and exceptions

        On Sep 28, 12:27 pm, robean <st1...@gmail.c omwrote:
        On Sep 28, 12:11 pm, "Chris Rebert" <c...@rebertia. comwrote:
        >
        >
        >
        On Sun, Sep 28, 2008 at 11:03 AM, robean <st1...@gmail.c omwrote:
        Hi everyone,
        >
        I have a question about using urllib2.
        >
        I like urllib2 better than urllib at least in part because it has more
        elaborate support for handling errors: there is built in support for
        URLError (for faulty urls) and HTTPError (for http errors that might
        originate from, say, passing an invalid stock-ticker in the program
        below).  However I can get neither to work.  I'm attaching below the
        (very short) code: can anyone point out what I'm doing wrong?
        >
        Now, if I replace the URLError and HTTPError with IOError (the class
        from which both URLError and HTTPError inherit), the program works
        fine. Why is it that I can call the generic IOError class, but none of
        the Error classes derived from that? These are clearly defined in the
        urllib2 manual. Very confused...
        >
        Here's the code:
        >
        import urllib2
        >
        # read stock information from yahoo finance for Traget (TGT)
        goodTicker = 'TGT' # program works with this
        badTicker = 'TGTttttttt' # python doesn't understand either HTTPError
        or URLError with this
        >
        url = "http://ichart.finance. yahoo.com/table.csv?s=" + badTicker
        >
        try:
               handle = urllib2.urlopen (url)
        >
        # this does not work
        except HTTPError, e:
               print "There was an http error"
               print e
        >
        # this also does not work
        except URLError, e:
               print "There is a problem with the URL"
               print e
               exit(1)
        >
        #this works
        except IOError, e:
               print "You have an IOError"
               print e
        >
        text = handle.readline s()[:20]
        for line in text:
               print line
        >>
        My Python begs to differ:
        >
        #tmp.py
        import urllib2
        >
        badTicker = 'TGTttttttt'
        url = "http://ichart.finance. yahoo.com/table.csv?s=" + badTicker
        >
        try:
            handle = urllib2.urlopen (url)
        >
        except urllib2.HTTPErr or, e:
            print "There was an http error"
            print e
        >
        except urllib2.URLErro r, e:
            print "There is a problem with the URL"
            print e
        >
        except urllib2.IOError , e:
            print "You have an IOError"
            print e
        >
        #in the shell
        $ python -V
        Python 2.5.1
        $ python Desktop/tmp.py
        There was an http error
        HTTP Error 404: Not Found
        >
        Are you using an outdated version of Python perhaps?
        >
        Regards,
        Chris
        >
        --
        Follow the path of the Iguana...http://rebertia.com
        >
        Then I expect that it is most likely my version of python that is
        causing the problem. I'm using 2.5.2.
        Actually, the problem seems to be that IOError is in my namespace, but
        the other error classes are not. So,

        except HTTPError, etc.

        fails, but

        except urllib2.HttpErr or, etc.

        works fine. Now, I still don't understand why these classes shouldn't
        automatically work....

        Comment

        • alex23

          #5
          Re: urllib2 and exceptions

          On Sep 29, 5:52 am, robean <st1...@gmail.c omwrote:
          Actually, the problem seems to be that IOError is in my namespace, but
          the other error classes are not. So,
          >
             except HTTPError, etc.
          >
          fails, but
          >
             except urllib2.HttpErr or, etc.
          >
          works fine. Now, I still don't understand why these classes shouldn't
          automatically work....
          IOError is a standard Python exception. HTTPError & URLError are
          exceptions provided by the urllib2 module. They need to be imported
          from or referenced through urllib2 to be used.

          Comment

          • robean

            #6
            Re: urllib2 and exceptions

            On Sep 28, 5:33 pm, alex23 <wuwe...@gmail. comwrote:
            On Sep 29, 5:52 am, robean <st1...@gmail.c omwrote:
            >
            Actually, the problem seems to be that IOError is in my namespace, but
            the other error classes are not. So,
            >
               except HTTPError, etc.
            >
            fails, but
            >
               except urllib2.HttpErr or, etc.
            >
            works fine. Now, I still don't understand why these classes shouldn't
            automatically work....
            >
            IOError is a standard Python exception. HTTPError & URLError are
            exceptions provided by the urllib2 module. They need to be imported
            from or referenced through urllib2 to be used.
            Many thanks for your reply. I was simply under the impression that
            'import urllib2' would take care of the namespace issue and simply
            import everything in urlib2, making it unnecessary to have to
            reference HTTPError and URLError. Sorry for being dense about this
            (I'm very new to Python).. Again, thanks for your help.

            Comment

            • Gabriel Genellina

              #7
              Re: urllib2 and exceptions

              En Sun, 28 Sep 2008 22:44:20 -0300, robean <st1999@gmail.c omescribió:
              Many thanks for your reply. I was simply under the impression that
              'import urllib2' would take care of the namespace issue and simply
              import everything in urlib2, making it unnecessary to have to
              reference HTTPError and URLError. Sorry for being dense about this
              (I'm very new to Python).. Again, thanks for your help.
              That's a common misconception - see this article:


              --
              Gabriel Genellina

              Comment

              Working...