禁用特殊 "class" 属性处理

Disable special "class" attribute handling

故事:

当您使用 BeautifulSoup 解析 HTML 时,class 属性被视为 multi-valued attribute 并以特殊方式处理:

Remember that a single tag can have multiple values for its “class” attribute. When you search for a tag that matches a certain CSS class, you’re matching against any of its CSS classes.

此外,BeautifulSoup 使用内置 HTMLTreeBuilder 作为其他树构建器 classes 的基础,例如 HTMLParserTreeBuilder :

# The HTML standard defines these attributes as containing a
# space-separated list of values, not a single value. That is,
# class="foo bar" means that the 'class' attribute has two values,
# 'foo' and 'bar', not the single value 'foo bar'.  When we
# encounter one of these attributes, we will parse its value into
# a list of values if possible. Upon output, the list will be
# converted back into a string.

问题:

如何配置 BeautifulSoup 以将 class 作为通常的单值属性处理?换句话说,我不希望它专门处理 class 并将其视为常规属性。

仅供参考,这是一个有用的用例:

我试过的:

实际上,我已经通过制作 自定义树构建器 class 并从特殊处理属性列表中删除 class 使其工作:

from bs4.builder._htmlparser import HTMLParserTreeBuilder

class MyBuilder(HTMLParserTreeBuilder):
    def __init__(self):
        super(MyBuilder, self).__init__()

        # BeautifulSoup, please don't treat "class" specially
        self.cdata_list_attributes["*"].remove("class")


soup = BeautifulSoup(data, "html.parser", builder=MyBuilder())

我不喜欢这种方法的地方在于它 "unnatural" 和 "magical" 涉及导入 "private" 内部 _htmlparser。我希望有一个更简单的方法。

注意:我想保存所有其他 HTML 解析相关功能,这意味着我不想解析 HTML 与 "xml"-only 功能(这可能是另一种解决方法)。

classHTMLParserTreeBuilder实际上是在upper module_init__.py上声明的,所以不需要直接从私有子模块导入。那就是说我会按照以下方式进行:

import re

from bs4 import BeautifulSoup
from bs4.builder import HTMLParserTreeBuilder

bb = HTMLParserTreeBuilder()
bb.cdata_list_attributes["*"].remove("class")

soup = BeautifulSoup(bs, "html.parser", builder=bb)
found_elements = soup.find_all(class_=re.compile(r"^name\-single name\d+$"))
print found_elements

这与在 OP 中定义 class 基本相同(可能更明确一点),但我认为没有更好的方法。

What I don't like in this approach is that it is quite "unnatural" and "magical" involving importing "private" internal _htmlparser. I hope there is a simpler way.

是的,您可以从 bs4.builder 导入它:

from bs4 import BeautifulSoup
from bs4.builder import HTMLParserTreeBuilder

class MyBuilder(HTMLParserTreeBuilder):
    def __init__(self):
        super(MyBuilder, self).__init__()
        # BeautifulSoup, please don't treat "class" as a list
        self.cdata_list_attributes["*"].remove("class")


soup = BeautifulSoup(data, "html.parser", builder=MyBuilder())

如果它足够重要以至于您不想重复自己的话,请将构建器放在它自己的模块中,并使用 register_treebuilders_from() 注册它,以便它具有优先权。