BeautifulSoup 解析 unicode 给出可变结果

BeautifulSoup parsing unicode giving variable results

我正在尝试解析以下 ipython 笔记本,但是当我将 unicode 读入 BeautifulSoup 对象时,我得到了不同的结果,即

from IPython.nbconvert.exporters import HTMLExporter
from IPython.config import Config
from bs4 import BeautifulSoup

filepath = '2015-05-01_test2.ipynb'
config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                 'highlight_class': '.highlight-ipynb'}})
exporter = HTMLExporter(config=config, template_file='basic')
content, info = exporter.from_filename(filepath)

soup = BeautifulSoup(content)
soup2 = BeautifulSoup(content)
soup3 = BeautifulSoup(content)
soup4 = BeautifulSoup(content)

print(soup == soup2)
print(soup == soup3)
print(soup == soup4)

给出输出

False
True
True

但是,如果我多次 运行 以下代码片段,输出会不断地在一组不同的 True/False 组合之间切换。我对可能导致这种情况的原因感到非常茫然,以前有人 运行 遇到过此类问题吗?

可以找到相关笔记本here

编辑

经进一步调查,这个问题似乎只发生在 Linux。我已经在 Linux Mint 和 Ubuntu 以及两台 windows 机器上试过了,这个问题似乎只发生在 Linux 上。请注意,我使用的是 IPython 3.0.0 和 bs4 4.3.2

RTM 的另一种情况,指定要使用的特定解析器似乎可以解决问题。

soup = BeautifulSoup(content, 'html.parser')

soup = BeautifulSoup(content, 'xml')