如何在Python Mechanize中修复编码?

Flu*_*ffy 5 python encoding mechanize

这是示例代码:

from mechanize import Browser

br = Browser()
page = br.open('http://hunters.tclans.ru/news.php?readmore=2')
br.form = br.forms().next()
print br.form
Run Code Online (Sandbox Code Playgroud)

问题是服务器返回不正确的编码(windows-cp1251).如何在mechanize中手动设置当前页面的编码?

错误:

Traceback (most recent call last):
  File "/tmp/stackoverflow.py", line 5, in <module>
    br.form = br.forms().next()
  File "/usr/local/lib/python2.6/dist-packages/mechanize/_mechanize.py", line 426, in forms
    return self._factory.forms()
  File "/usr/local/lib/python2.6/dist-packages/mechanize/_html.py", line 559, in forms
    self._forms_factory.forms())
  File "/usr/local/lib/python2.6/dist-packages/mechanize/_html.py", line 225, in forms
    _urlunparse=_rfc3986.urlunsplit,
  File "/usr/local/lib/python2.6/dist-packages/ClientForm.py", line 967, in ParseResponseEx
    _urlunparse=_urlunparse,
  File "/usr/local/lib/python2.6/dist-packages/ClientForm.py", line 1104, in _ParseFileEx
    fp.feed(data)
  File "/usr/local/lib/python2.6/dist-packages/ClientForm.py", line 870, in feed
    sgmllib.SGMLParser.feed(self, data)
  File "/usr/lib/python2.6/sgmllib.py", line 104, in feed
    self.goahead(0)
  File "/usr/lib/python2.6/sgmllib.py", line 193, in goahead
    self.handle_entityref(name)
  File "/usr/local/lib/python2.6/dist-packages/ClientForm.py", line 751, in handle_entityref
    '&%s;' % name, self._entitydefs, self._encoding))
  File "/usr/local/lib/python2.6/dist-packages/ClientForm.py", line 238, in unescape
    return re.sub(r"&#?[A-Za-z0-9]+?;", replace_entities, data)
  File "/usr/lib/python2.6/re.py", line 151, in sub
    return _compile(pattern, 0).sub(repl, string, count)
  File "/usr/local/lib/python2.6/dist-packages/ClientForm.py", line 230, in replace_entities
    repl = repl.encode(encoding)
LookupError: unknown encoding: windows-cp1251
Run Code Online (Sandbox Code Playgroud)

Flu*_*ffy 2

通过设置固定

br._factory.encoding = enc
br._factory._forms_factory.encoding = enc
br._factory._links_factory._encoding = enc
Run Code Online (Sandbox Code Playgroud)

(注意下划线)在 br.open() 之后