2016-08-22 161 views
3

我試圖安裝html5lib。起初我試圖安裝最新版本(8或9個9),但它與我的BeautifulSoup發生衝突,所以我決定嘗試舊的版本(0.9999999,seven nines)。我安裝了它,但是當我嘗試使用它:html5lib:TypeError:__init __()得到了意想不到的關鍵字參數'encoding'

>>> 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' 

什麼是錯的,我該怎麼辦?

回答

6

我看到一些在html5lib的最新版本打破了關於BS4,html5lib.treebuilders._base不再存在,USNG BS4 4.4.1最新的兼容版本似乎是一個有7個花枝招展,一旦你安裝它,如下正常工作:

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.pyHTMLBinaryInputStream沒有編碼ARG:

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]: 
相關問題