The following code:
#!/usr/bin/env python
import mechanize
class MechanizeSubclass(mechanize.Browser):
def __init__(self,
factory=None,
history=None,
request_class=None,
):
mechanize.Browser.__init__(self, factory, history, request_class)
def open(self, url, data=None,
timeout=mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
mechanize.Browser.open(self, url, data, timeout=timeout)
subclass = MechanizeSubclass()
subclass.open('https://uncjobs.northcarolina.edu/applicants/jsp/shared/Welcome_css.jsp')
print subclass.response().read()
generates the error
mechanize._response.httperror_seek_wrapper: HTTP Error 302: Moved Temporarily
I looked at the mechanize code and the Browser.open() method is defined as:
def open(self, url, data=None,
timeout=_sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
return self._mech_open(url, data, timeout=timeout)
and if I change the open() method in my subclass to match this:
class MechanizeSubclass(mechanize.Browser):
...
def open(self, url, data=None,
timeout=mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
return self._mech_open(url, data, timeout=timeout)
then it works fine. But I still don’t really understand why the first definition using mechanize.Browser.open(self, url, data, timeout=timeout) doesn’t work. Shouldn’t they be equivalent? This is with python 2.6 with mechanize 0.2.5.
The main difference between the first code snippet and the other two is that the
openmethod is not returning anything (which in Python isthe same as returning theNoneobject).That is, whatever code is calling the
openmethod expects to have the object returned by_mech_open. Your first method just returns nothing.If you simply change the first implementation to:
You should not have this problem.