web-scraping - html5lib: TypeError: __init__() 得到一个意外的关键字参数 'encoding'

标签 web-scraping beautifulsoup html5lib

我正在尝试安装 html5lib .起初我尝试安装最新版本(8 个或 9 个 9),但它与我的 BeautifulSoup 冲突,所以我决定尝试旧版本(0.9999999,seven nines)。我安装了它,但是当我尝试使用它时:

>>> with urlopen("http://example.com/") as f:
    document = html5lib.parse(f, encoding=f.info().get_content_charset())

我收到一个错误:
Traceback (most recent call last):
  File "<pyshell#11>", line 2, in <module>
    document = html5lib.parse(f, encoding=f.info().get_content_charset())
  File "C:\Python\Python35-32\lib\site-packages\html5lib\html5parser.py", line 35, in parse
    return p.parse(doc, **kwargs)
  File "C:\Python\Python35-32\lib\site-packages\html5lib\html5parser.py", line 235, in parse
    self._parse(stream, False, None, *args, **kwargs)
  File "C:\Python\Python35-32\lib\site-packages\html5lib\html5parser.py", line 85, in _parse
    self.tokenizer = _tokenizer.HTMLTokenizer(stream, parser=self, **kwargs)
  File "C:\Python\Python35-32\lib\site-packages\html5lib\_tokenizer.py", line 36, in __init__
    self.stream = HTMLInputStream(stream, **kwargs)
  File "C:\Python\Python35-32\lib\site-packages\html5lib\_inputstream.py", line 151, in HTMLInputStream
    return HTMLBinaryInputStream(source, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'encoding'

出了什么问题,我该怎么办?

最佳答案

我看到在最新版本的 html5lib 中关于 bs4 的东西被破坏了,html5lib.treebuilders._base 不再存在,使用 bs4 4.4.1 最新的兼容版本似乎是有 7 个 9 的版本,一旦你如下安装它它工作正常:

 pip3 install -U html5lib=="0.9999999"

使用 bs4 4.4.1 测试:
In [1]: import bs4

In [2]: bs4.__version__
Out[2]: '4.4.1'

In [3]: import html5lib

In [4]: html5lib.__version__
Out[4]: '0.9999999'

In [5]: from urllib.request import  urlopen

In [6]: with urlopen("http://example.com/") as f:
   ...:         document = html5lib.parse(f, encoding=f.info().get_content_charset())
   ...:     

In [7]: 

您可以在此提交中看到更改 Rename treebuilders._base to .base to reflect public status名称已更改:

您看到的错误是因为您仍在使用最新版本,在 html5lib/_inputstream.py 中,HTMLBinaryInputStream 没有编码参数:
class HTMLBinaryInputStream(HTMLUnicodeInputStream):
    """Provides a unicode stream of characters to the HTMLTokenizer.

    This class takes care of character encoding and removing or replacing
    incorrect byte-sequences and also provides column and line tracking.

    """

    def __init__(self, source, override_encoding=None, transport_encoding=None,
                 same_origin_parent_encoding=None, likely_encoding=None,
                 default_encoding="windows-1252", useChardet=True):

设置 override_encoding=f.info().get_content_charset() 应该可以解决问题。

升级到最新版本的 bs4 也适用于最新版本的 html5lib:
In [16]: bs4.__version__
Out[16]: '4.5.1'

In [17]: html5lib.__version__
Out[17]: '0.999999999'

In [18]: with urlopen("http://example.com/") as f:
             document = html5lib.parse(f, override_encoding=f.info().get_content_charset())
   ....:     

In [19]: 

关于web-scraping - html5lib: TypeError: __init__() 得到一个意外的关键字参数 'encoding',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39086278/

相关文章:

python - 使用 Python 从数据框的列中删除停用词

python - 如何从此网络服务下载多页数据?

python - 使用 BeautifulSoup 的不同 XML 元素名称列表

python - ImportError:html5lib 中没有名为 base 的模块

python - bs4.FeatureNotFound : Couldn't find a tree builder with the features you requested: html5lib

python - 在非英文字符站点中抓取数据的问题 [Python]

python - 如何从 <select> 标签中抓取更改内容的网页

python - 刮过无限滚动条

python - 如何获取 "subsoups"并连接/加入它们?

python - 禁用 lxml 中 '--' 的注释检查