473,657 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

--
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.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
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.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
--
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.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.
Sep 28 '08 #3
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
--
>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.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....
Sep 28 '08 #4
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.
Sep 29 '08 #5
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.
Sep 29 '08 #6
En Sun, 28 Sep 2008 22:44:20 -0300, robean <st****@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:
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
6094
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 #80 and line # 38 are the first line offenders. --> --> -->
1
4564
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 problem, but I'd like to have more control over the process. In particular I'd like to get back the HTTP status code from the request, even if it's a 200. It looks like I can do that by deriving my own class from HTTPHandler, but I'm not sure how to go...
5
7395
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. here's my pyscript:
0
2529
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 because it throws exceptions which are important to be handled where as urlretrieve is dumb.
2
1783
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 information about the error? --
6
2357
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 problem also with other imports (e.g. urllib2 etc.). And again very often in all these cases where I get weired Python exceptions, the problem is around re-functions - usually during re.compile calls during import (see some of the exceptions below). But...
1
5752
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 url="http://localhost/private/file" #proxy set up
10
3565
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. File "D:\Python24\lib\socket.py", line 295, in read data = self._sock.recv(recv_size) timeout: timed out
3
5759
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 (compatible; MSIE 5.5; Windows NT)' headers = {'User-Agent':user_agent} request = urllib2.Request(url, headers=headers) try:
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.