禁用特殊“类"属性处理 [英] Disable special "class" attribute handling

查看:13
本文介绍了禁用特殊“类"属性处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

故事:

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

When you parse HTML with BeautifulSoup, class attribute is considered a multi-valued attribute and is handled in a special manner:

请记住,单个标签的class"属性可以有多个值.当您搜索与某个 CSS 类匹配的标签时,您正在匹配其任何 CSS 类.

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 作为其他树构建器类的基础,例如 HTMLParserTreeBuilder:

Also, a quote from a built-in HTMLTreeBuilder used by BeautifulSoup as a base for other tree builder classes, like, for instance, 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 并将其视为常规属性.

How can I configure BeautifulSoup to handle class as a usual single-valued attribute? In other words, I don't want it to handle class specially and consider it a regular attribute.

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

FYI, here is one of the use-cases when it can be helpful:

我尝试过的:

我实际上是通过创建一个自定义树构建器类并从特殊处理的属性列表中删除class来使其工作的:

I've actually made it work by making a custom tree builder class and removing class from the list of specially-handled attributes:

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())

我不喜欢这种方法的一点是它非常不自然"和神奇",涉及导入私有"内部_htmlparser.我希望有更简单的方法.

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.

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

NOTE: I want to save all other HTML parsing related features, meaning I don't want to parse HTML with "xml"-only features (which could've been another workaround).

推荐答案

我不喜欢这种方法的一点是它非常不自然"和神奇",涉及导入私有"内部_htmlparser.我希望有更简单的方法.

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 导入:

Yes, you can import it from bs4.builder instead:

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() 注册它,以便它具有优先权.

And if it's important enough that you don't want to repeat yourself, put the builder in its own module, and register it with register_treebuilders_from() so that it takes precedence.

这篇关于禁用特殊“类"属性处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆