gpt4 book ai didi

python - 禁用特殊的 "class"属性处理

转载 作者:技术小花猫 更新时间:2023-10-29 12:21:04 27 4
gpt4 key购买 nike

故事:

当您使用 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 的引用作为其他树构建器类的基础,例如 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使其工作:

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

我不喜欢这种方法的地方在于,它涉及导入“私有(private)”内部 _htmlparser 是相当“不自然”和“神奇”的。我希望有更简单的方法。

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

最佳答案

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

关于python - 禁用特殊的 "class"属性处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295928/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com