views:

435

answers:

2

Hello, here is the sample code:

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

The problem is that server return incorrect encoding (windows-cp1251). How can I manually set the encoding of the current page in mechanize?

Error:

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
+3  A: 

I don't know about Mechanize, but you could hack codecs to accept wrong encoding names that have both ‘windows’ and ‘cp’:

>>> def fixcp(name):
...     if name.lower().startswith('windows-cp'):
...         try:
...             return codecs.lookup(name[:8]+name[10:])
...         except LookupError:
...             pass
...     return None
... 
>>> codecs.register(fixcp)
>>> '\xcd\xe0\xef\xee\xec\xe8\xed\xe0\xe5\xec'.decode('windows-cp1251')
u'Напоминаем'
bobince
Getting which value was meant is not a problem. Question now is how to access _AbstractFormParser of mechanize's entity.
roddik
A: 

Fixed by setting

br._factory.encoding = enc
br._factory._forms_factory.encoding = enc
br._factory._links_factory._encoding = enc

(note the underscores) after br.open()

roddik