[Tutor] urllib2.urlopen()

Brian van den Broek brian.van.den.broek at gmail.com
Sun Oct 14 02:09:08 CEST 2012

On 13 October 2012 19:44, Ray Jones <crawlzone at gmail.com> wrote:
> I am attempting to capture url headers and have my script make decisions
> based on the content of those headers.
> Here is what I am using in the relative portion of my script:
> try:
>     urllib2.urlopen('http://myurl.org')
> except urllib2.HTTPError, e:
> In the case of authentication error, I can print e.info() and get all
> the relevant header information. But I don't want to print. I want the
> information from the instance available to use in my script. How do I
> accomplish that?
> Ray

Hi Ray,

(Sorry for the double message, Ray. I forgot to Reply to all.)

I'm not very familiar with using urllib2 and I've never used it with a
page which requires authentication. So, this might not sort you. But,
perhaps it will get you started on how to figure it out for yourself:

>>> import urllib2
>>> E = None
>>> try:
except urllib2.URLError, e:
        print 42
        E = e

>>> print dir(E)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__getitem__', '__getslice__', '__hash__',
'__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__',
'__subclasshook__', '__unicode__', '__weakref__', 'args', 'errno',
'filename', 'message', 'reason', 'strerror']


Brian vdB

More information about the Tutor mailing list