【问题标题】:Converting from old BeautifulSoup format to the new从旧的 BeautifulSoup 格式转换为新格式
【发布时间】:2015-02-15 17:07:41
【问题描述】:

我找到了一个较旧的 Python 代码,其中包含这一行:

    return BeautifulSoup(m.group(1),
            convertEntities=BeautifulSoup.HTML_ENTITIES).contents[0].string

当我尝试运行代码时出现此错误:

AttributeError: type object 'BeautifulSoup' has no attribute 'HTML_ENTITIES

谁能解释一下这行的意思:

BeautifulSoup(m.group(1),convertEntities=BeautifulSoup.HTML_ENTITIES).contents[0].string

以及如何在新版本中使用 formater 来做到这一点?

我知道有一个类似的问题在这里回答: BeautifulSoup' has no attribute 'HTML_ENTITIES 但由于我不熟悉 BeautifulSoup,我无法将行转换为新格式。

非常感谢您对此的任何帮助。

【问题讨论】:

    标签: python html python-2.7 beautifulsoup


    【解决方案1】:

    您根本不需要在此处指定 convertEntities,该行为(将 HTML 实体转换为其 Unicode 代码点)现在是默认值

    【讨论】:

    • 那么你能写出新版本BeautifulSoup中的代码应该是什么样子吗?
    • 只需删除convertEntities kwargs,即BeautifulSoup(m.group(1)).contents[0].string
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2021-10-02
    • 2021-02-28
    相关资源
    最近更新 更多