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

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

问题描述

的故事:

当您解析与 BeautifulSoup HTML 属性被认为是的multi-valued属性并以特殊方式处理:


  

请记住,一个标签可以有它的class属性的多个值。当您搜索匹配特定CSS类的标记,你对匹配任何CSS类的。


此外,从报价内置使用 BeautifulSoup 作为其它树构建器类的基 HTMLTreeBuilder 一样,例如, HTMLParserTreeBuilder

 #HTML标准定义了这些属性为包含
值#空格分隔的列表,而不是单个值。那是,
#类=富巴的意思是class属性有两个值,
#'富'和'酒吧',而不是单个值'富巴。什么时候我们
#遭遇这些属性之一,我们将解析它的值写入
#如果可能值的列表。在输出时,该列表会
#转换回字符串。

问题:

如何配置 BeautifulSoup 来处理作为通常的单值属性?换句话说,我不希望它处理的特别并认为这是一个普通的属性。

仅供参考,这里的用例时,它可以帮助之一:

我已经试过:

其实我已经使它通过工作的自定义树生成器类的和删除从专门处理的属性列表:

 从bs4.builder._htmlparser进口HTMLParserTreeBuilder类MyBuilder(HTMLParserTreeBuilder):
    高清__init __(个体经营):
        超(MyBuilder,个体经营).__的init __()        #BeautifulSoup,请不要把阶级专
        self.cdata_list_attributes [*]。删除(类)
汤= BeautifulSoup(数据,html.parser,建设者= MyBuilder())

我不这种方法一样的是,它是相当不自然和神奇的进口涉及到私人内部 _htmlparser 。我希望有一个简单的方法。

注:我想保存所有其他HTML解析相关功能,这意味着我不想来解析 HTML 与XML - 只功能(这可以一直另一个解决办法)。


解决方案

  

我不这种方法一样的是,它是相当不自然和神奇的进口涉及到私人内部 _htmlparser 。我希望有一个简单的方法。


是的,你可以从 bs4.builder 导入它来代替:

 从bs4.builder进口HTMLParserTreeBuilder类MyBuilder(HTMLParserTreeBuilder):
    高清__init __(个体经营):
        超(MyBuilder,个体经营).__的init __()
        #BeautifulSoup,请不要把级作为一个列表
        self.cdata_list_attributes [*]。删除(类)
汤= BeautifulSoup(数据,html.parser,建设者= MyBuilder())

如果它足够重要,你不想重复自己,把建造自己的模块中,并用 register_treebuilders_from注册它()因此,它需要precedence。

The Story:

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

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.

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.

The Question:

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:

What I've tried:

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

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.

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

解决方案

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.

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

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

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天全站免登陆