BeautifulSoup复方类名称搜索时返回空列表 [英] BeautifulSoup returns empty list when searching by compound class names

查看:1138
本文介绍了BeautifulSoup复方类名称搜索时返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

BeautifulSoup returns empty list when searching by compound class names using regex.

例如:

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*)$"))

我需要的类选择是非常precise。任何想法?

I need the class selection to be very precise. Any ideas?

推荐答案

不幸的是,当您试图使在包含多个类的类属性值的常规前pression比赛, BeautifulSoup 将分别应用常规的前pression到每一个班级。以下是有关该问题的相关内容:

Unfortunately, when you try to make a regular expression match on a class attribute value that contains multiple classes, BeautifulSoup would apply the regular expression to every single class separately. Here are the relevant topics about the problem:

  • Python regular expression for Beautiful Soup
  • Multiple CSS class search is unhandy

这一切都是因为 是一个很特别的多值属性以及每次解析HTML时, BeautifulSoup 的树建设者之一(根据解析器选择)内部将一个类的字符串值入类(从 HTMLTreeBuilder 的文档字符串引号)的列表:

This is all because class is a very special multi-valued attribute and every time you parse HTML, one of the BeautifulSoup's tree builders (depending on the parser choice) internally splits a class string value into a list of classes (quote from the HTMLTreeBuilder's docstring):

# 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 不办理:由我们制作简单的自定义树构建一个多值属性:


There are multiple workarounds, but here is a hack-ish one - we are going to ask BeautifulSoup not to handle class as a multi-valued attribute by making our simple custom tree builder:

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)

在这种情况下,常规的前pression将被应用到属性值作为一个整体。

In this case the regular expression would be applied to a class attribute value as a whole.

另外,你可以只用解析 XML 的HTML功能启用(如果适用):

Alternatively, you can just parse the HTML with xml features enabled (if this is applicable):

soup = BeautifulSoup(data, "xml")


您也可以使用 CSS选择器并匹配所有

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

如果需要的话

您可以手动应用常规的前pression:

You can then apply the regular expression manually if needed:

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)

这篇关于BeautifulSoup复方类名称搜索时返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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