How to fix encoding in Python Mechanize?

here is a 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 the server is returning the wrong encoding (windows-cp1251). How to manually set the encoding of the current page in the mechanization?

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 
+5
source share
2 answers

Fixed by installation

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

(note the underscores) after br.open ()

+2
source

I don’t know about Mechanize, but you can hack codecs to accept incorrect 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'' 
+3
source

Source: https://habr.com/ru/post/892231/


All Articles