SquidsEnMasse
SquidsEnMasse

Reputation: 716

urllib2 get response through error

I am trying to log onto a website that throws me a "401: Authentication Required" error and then crashes. The script crashes on the urllib2.open(url) so I cannot currently read the response. How can I get the data from this response other than the error?

Upvotes: 1

Views: 253

Answers (1)

Okto
Okto

Reputation: 96

Take a look at http://docs.python.org/howto/urllib2.html

    >>> req = urllib2.Request('http://www.python.org/fish.html')
    >>> try:
    >>>     urllib2.urlopen(req)
    >>> except HTTPError, e:
    >>>     print e.code
    >>>     print e.read()
    >>>
    404
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <?xml-stylesheet href="./css/ht2html.css" type="text/css"?>
    ...... etc...

Upvotes: 1

Related Questions