473,382 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Sep 28 '08 #1
6 8784
On Sun, Sep 28, 2008 at 11:03 AM, robean <st****@gmail.comwrote:
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

--
http://mail.python.org/mailman/listinfo/python-list
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

--
Follow the path of the Iguana...
http://rebertia.com
Sep 28 '08 #2
On Sep 28, 12:11*pm, "Chris Rebert" <c...@rebertia.comwrote:
On Sun, Sep 28, 2008 at 11:03 AM, robean <st1...@gmail.comwrote:
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
--
http://mail.python.org/mailman/listinfo/python-list

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

--
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.
Sep 28 '08 #3
On Sep 28, 12:27*pm, robean <st1...@gmail.comwrote:
On Sep 28, 12:11*pm, "Chris Rebert" <c...@rebertia.comwrote:
On Sun, Sep 28, 2008 at 11:03 AM, robean <st1...@gmail.comwrote:
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
--
>http://mail.python.org/mailman/listinfo/python-list
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
--
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.HttpError, etc.

works fine. Now, I still don't understand why these classes shouldn't
automatically work....
Sep 28 '08 #4
On Sep 29, 5:52*am, robean <st1...@gmail.comwrote:
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.
Sep 29 '08 #5
On Sep 28, 5:33*pm, alex23 <wuwe...@gmail.comwrote:
On Sep 29, 5:52*am, robean <st1...@gmail.comwrote:
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.
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.
Sep 29 '08 #6
En Sun, 28 Sep 2008 22:44:20 -0300, robean <st****@gmail.comescribió:
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

--
Gabriel Genellina

Sep 30 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John F Dutcher | last post by:
Can anyone comment on why the code shown in the Python error is in some way incorrect...or is there a problem with Python on my hoster's site ?? The highlites don't seem to show here...but line...
1
by: Doug Farrell | last post by:
Hi all, I'm trying to build a web page crawler to help us build our websites, which are driven by static pages after they are called the first time. Anyway, I can use urllib2.urlopen() no...
5
by: Pascal | last post by:
Hello, I want to acces my OWA (Outlook Web Acces - http Exchange interface) server with urllib2 but, when I try, I've always a 401 http error. Can someone help me (and us)? Thanks. ...
0
by: Ritesh Raj Sarraf | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, In urllib.urlretrieve I can use reporthook to implement a progress bar. But in urllib2.urlopen I can't. I have to use urllib2.urlopen...
2
by: Gregory Piñero | last post by:
How can I catch 2 exceptions at once for example: try: self.gses = opener.open(req) except (urllib2.HTTPError,urllib2.URLError): do something.. Seems to work, but how do I also get...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
1
by: Alessandro Fachin | last post by:
I write this simply code that should give me the access to private page with htaccess using a proxy, i don't known because it's wrong... import urllib,urllib2 #input url...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
3
by: konstantin | last post by:
Hi, I wonder if there is a safe way to download page with urllib2. I've constructed following method to catch all possible exceptions. def retrieve(url): user_agent = 'Mozilla/4.0...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.