如何修复"意外的关键字参数' useChardet'"在html5lib中

时间:2016-07-25 20:41:56

标签: python html5lib

我使用html5lib并在将其更新到最新版本后,我不断收到此错误:

Traceback (most recent call last):
  File "/home/travis/build/freelawproject/juriscraper/tests/test_everything.py", line 119, in test_scrape_all_example_files
    site.parse()
  File "/home/travis/build/freelawproject/juriscraper/juriscraper/AbstractSite.py", line 95, in parse
    self.html = self._download()
  File "/home/travis/build/freelawproject/juriscraper/juriscraper/AbstractSite.py", line 384, in _download
    html_tree = self._make_html_tree(text)
  File "/home/travis/build/freelawproject/juriscraper/juriscraper/opinions/united_states/federal_appellate/ca11_u.py", line 26, in _make_html_tree
    e = html5parser.document_fromstring(text)
  File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/lxml/html/html5parser.py", line 64, in document_fromstring
    return parser.parse(html, useChardet=guess_charset).getroot()
  File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/html5lib/html5parser.py", line 235, in parse
    self._parse(stream, False, None, *args, **kwargs)
  File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/html5lib/html5parser.py", line 85, in _parse
    self.tokenizer = _tokenizer.HTMLTokenizer(stream, parser=self, **kwargs)
  File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/html5lib/_tokenizer.py", line 36, in __init__
    self.stream = HTMLInputStream(stream, **kwargs)
  File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/html5lib/_inputstream.py", line 149, in HTMLInputStream
    return HTMLUnicodeInputStream(source, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'useChardet'

我使用的代码非常简单:

from lxml.html import html5parser
html5parser.document_fromstring(u'<html></html')

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

原来,如果你将一个unicode对象提供给document_fromstring方法,那就是barfs。它没有用,因为这只发生在我更新我的依赖项时。

无论如何,修复很简单:

html5parser.document_fromstring(u'<html></html'.encode('utf-8'))