今天想试着用一下BeautifulSoup,结果出师不利,刚导入就报错。在网上搜了下,有说要更新和重装BeautifulSoup的,但是我试了下,还是不行。去stackoverflow上终于找到了答案。

问题出现的原因是因为html5lib版本过高导致,所以解决办法有2个:
1.卸载掉当前的html5lib,安装更低版本:

pip uninstall html5lib
pip install html5lib==0.9999

2.修改bs4/builder/__init__.py文件,在文件的末尾,找到下面几行,注释掉:

from . import _htmlparser
register_treebuilders_from(_htmlparser)
try:
    from . import _html5lib
    register_treebuilders_from(_html5lib)
except ImportError:
    # They don't have html5lib installed.
    pass

我用的是方法2。问题解决。

Leave a Reply

Your email address will not be published. Required fields are marked *