Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors with Python's mechanize module

I'm using the mechanize module to execute some web queries from Python. I want my program to be error-resilient and handle all kinds of errors (wrong URLs, 403/404 responsese) gracefully. However, I can't find in mechanize's documentation the errors / exceptions it throws for various errors.

I just call it with:

    self.browser = mechanize.Browser()
    self.browser.addheaders = [('User-agent', browser_header)]

    self.browser.open(query_url)
    self.result_page = self.browser.response().read()

How can I know what errors / exceptions can be thrown here and handle them ?

like image 534
Eli Bendersky Avatar asked Sep 30 '08 06:09

Eli Bendersky


3 Answers

$ perl -0777 -ne'print qq($1) if /__all__ = \[(.*?)\]/s' __init__.py | grep Error 

'BrowserStateError',
'ContentTooShortError',
'FormNotFoundError',
'GopherError',
'HTTPDefaultErrorHandler',
'HTTPError',
'HTTPErrorProcessor',
'LinkNotFoundError',
'LoadError',
'ParseError',
'RobotExclusionError',
'URLError',

Or:

>>> import mechanize
>>> filter(lambda s: "Error" in s, dir(mechanize))
['BrowserStateError', 'ContentTooShortError', 'FormNotFoundError', 'GopherError'
, 'HTTPDefaultErrorHandler', 'HTTPError', 'HTTPErrorProcessor', 'LinkNotFoundErr
or', 'LoadError', 'ParseError', 'RobotExclusionError', 'URLError']
like image 190
jfs Avatar answered Oct 20 '22 18:10

jfs


While this has been posted a long time ago, I think there is still a need to answer the question correctly since it comes up in Google's search results for this very question.

As I write this, mechanize (version = (0, 1, 11, None, None)) in Python 265 raises urllib2.HTTPError and so the http status is available through catching this exception, eg:

import urllib2
try:
...  br.open("http://www.example.org/invalid-page")
... except urllib2.HTTPError, e:
...  print e.code
... 
404
like image 43
remote Avatar answered Oct 20 '22 17:10

remote


I found this in their docs:

One final thing to note is that there are some catch-all bare except: statements in the module, which are there to handle unexpected bad input without crashing your program. If this happens, it's a bug in mechanize, so please mail me the warning text.

So I guess they don't raise any exceptions. You can also search the source code for Exception subclasses and see how they are used.

like image 35
Alexander Kojevnikov Avatar answered Oct 20 '22 17:10

Alexander Kojevnikov