urllib2 and exceptions

R

robean

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.readlines()[:20]
for line in text:
print line
 
C

Chris Rebert

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.readlines()[: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.HTTPError, e:
print "There was an http error"
print e

except urllib2.URLError, 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
 
R

robean

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
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.readlines()[: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.HTTPError, e:
    print "There was an http error"
    print e

except urllib2.URLError, 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

Then I expect that it is most likely my version of python that is
causing the problem. I'm using 2.5.2.
 
R

robean

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.readlines()[: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.HTTPError, e:
    print "There was an http error"
    print e
except urllib2.URLError, 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?

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.HttpError, etc.

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

alex23

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.HttpError, 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.
 
R

robean

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.
 
G

Gabriel Genellina

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:
http://effbot.org/zone/import-confusion.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top