BeautifulSoup 在按复合类名搜索时返回空列表

2024-03-28

使用正则表达式按复合类名搜索时,BeautifulSoup 返回空列表。

Example:

import re
from bs4 import BeautifulSoup

bs = 
    """
    <a class="name-single name692" href="www.example.com"">Example Text</a>
    """

bsObj = BeautifulSoup(bs)

# this returns the class
found_elements = bsObj.find_all("a", class_= re.compile("^(name-single.*)$"))

# this returns an empty list
found_elements = bsObj.find_all("a", class_= re.compile("^(name-single name\d*)$"))

我需要非常精确的班级选择。有任何想法吗?


不幸的是,当您尝试对包含多个类的类属性值进行正则表达式匹配时,BeautifulSoup会将正则表达式分别应用于每个类。以下是有关该问题的相关主题:

  • Beautiful Soup 的 Python 正则表达式 https://stackoverflow.com/questions/13794532/python-regular-expression-for-beautiful-soup
  • 多个 CSS 类搜索不方便 https://bugs.launchpad.net/beautifulsoup/+bug/1157869

这都是因为class是一个非常特殊的多值属性 http://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-by-css-class每次解析 HTML 时,其中之一BeautifulSoup的树构建器(取决于解析器的选择)在内部将类字符串值拆分为类列表(引用自HTMLTreeBuilder的文档字符串):

# 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通过制作我们简单的自定义树构建器作为多值属性:

import re

from bs4 import BeautifulSoup
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")


bs = """<a class="name-single name692" href="www.example.com"">Example Text</a>"""
bsObj = BeautifulSoup(bs, "html.parser", builder=MyBuilder())
found_elements = bsObj.find_all("a", class_=re.compile(r"^name\-single name\d+$"))

print(found_elements)

在这种情况下,正则表达式将应用于class属性值作为一个整体。


或者,您可以使用以下命令解析 HTMLxml启用的功能(如果适用):

soup = BeautifulSoup(data, "xml")

您还可以使用CSS 选择器 http://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors并将所有元素与name-single类和以“name”开头的类:

soup.select("a.name-single,a[class^=name]")

然后,您可以根据需要手动应用正则表达式:

pattern = re.compile(r"^name-single name\d+$")
for elm in bsObj.select("a.name-single,a[class^=name]"):
    match = pattern.match(" ".join(elm["class"]))
    if match:
        print(elm)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

BeautifulSoup 在按复合类名搜索时返回空列表 的相关文章

随机推荐