次のコード:
#!/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()
エラーを生成します
mechanize._response.httperror_seek_wrapper: HTTP Error 302: Moved Temporarily
mechanizeコードを調べたところ、Browser.open()メソッドは次のように定義されています。
def open(self, url, data=None,
timeout=_sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
return self._mech_open(url, data, timeout=timeout)
サブクラスのopen()メソッドをこれに一致するように変更すると、次のようになります。
class MechanizeSubclass(mechanize.Browser):
...
def open(self, url, data=None,
timeout=mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
return self._mech_open(url, data, timeout=timeout)
その後、正常に動作します。しかし、mechanize.Browser.open(self、url、data、timeout = timeout)を使用した最初の定義が機能しない理由はまだよくわかりません。それらは同等であるべきではありませんか?これは、mechanize0.2.5を使用したpython2.6の場合です。