【问题标题】:BeautifulSoup parsing unicode giving variable resultsBeautifulSoup 解析 unicode 给出可变结果
【发布时间】:2015-08-08 21:56:15
【问题描述】:

我正在尝试解析以下 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

但是,如果我多次运行以下 sn-p,输出会不断在一组不同的真/假组合之间切换。我非常不知道是什么原因造成的,以前有没有人遇到过这种类型的问题?

相关笔记本可以找到here

编辑

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

【问题讨论】:

    标签: python-2.7 unicode beautifulsoup ipython-notebook


    【解决方案1】:

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

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

    soup = BeautifulSoup(content, 'xml')
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      相关资源
      最近更新 更多