【问题标题】:BeautifulSoup and class with spacesBeautifulSoup 和带空格的类
【发布时间】:2018-03-24 21:47:54
【问题描述】:

使用 BeautifulSoul 和 Python,我想 find_all 所有与给定类属性匹配的 tr 项,该类属性包含多个类似这样的名称:

<tr class="admin-bookings-table-row bookings-history-row  paid   ">

我尝试了几种方法来匹配该类。正则表达式、通配符,但我总是得到一个空列表。

有没有办法使用正则表达式、通配符或者如何匹配这个类?

同样的问题here 已发布,但没有答案。

【问题讨论】:

  • 为了记录,一个类中不能有空格。这个元素有多个类。

标签: python beautifulsoup


【解决方案1】:

您可以使用css selector 匹配多个类:

from bs4 import BeautifulSoup as soup
html = '''
<tr class="admin-bookings-table-row bookings-history-row  paid   "></tr>
<tr class="admin-bookings-table-row  nope  paid   "></tr>
'''
soup = soup(html, 'lxml')

res = soup.select('tr.admin-bookings-table-row.bookings-history-row.paid')
print(res)

>>> [<tr class="admin-bookings-table-row bookings-history-row paid "></tr>]

否则,也许这个答案也可以帮助你: https://stackoverflow.com/a/46719501/6655211

【讨论】:

  • 有趣的方法。 css 选择器可以只返回像soup.find() 这样的第一个匹配项吗?
  • Ofc,你可以试试select_one
【解决方案2】:

HTML 类不能包含空格。这个元素有多个类。

通过这些类中的任何一个搜索都可以:

from bs4 import BeautifulSoup

html = '<tr id="history_row_938220" style="" class="admin-bookings-table-row bookings-history-row  paid   ">'


soup = BeautifulSoup(html, 'html.parser')

print(soup.find_all(attrs={'class': 'admin-bookings-table-row'}))
print(soup.find_all(attrs={'class': 'bookings-history-row'}))
print(soup.find_all(attrs={'class': 'paid'}))

所有输出

[<tr class="admin-bookings-table-row bookings-history-row paid " 
 id="history_row_938220" style=""></tr>]

【讨论】:

  • 问题似乎是找到所有具有多个类的tr 项目。我不确定这是否能找到。
  • @BradSolomon 我刚刚展示了使用 3 个类中的 either 可以找到元素,所以我不确定 OP 正在做什么以获得空列表。
  • 这不是问题。 “我想查找所有包含多个空格的给定类的所有 tr 项目。”如果你有一个标签 class="paid",你的 attrs 过滤器会返回它,即使它只有 1 个类。
  • @BradSolomon 现在我们进入语义。 "I want to find_all all tr items with a given class that contain multiple spaces." 根据定义是错误的(也是不可能的),因为没有 "a given class that contain multiple spaces" 这样的东西。 Beautiful Soup 在按类搜索时使用包含逻辑(通过传递类列表可以实现与上述相同的行为:soup.find_all(attrs={'class': ['admin-bookings-table-row', 'bookings-history-row', 'paid']})
  • 哈。我知道这一点,但其意图似乎显然是将多个空间等同于多个类。让我们试着澄清一下。
【解决方案3】:

我想find_all 所有tr 具有给定类的项目包含 多个空格。

多个空格实际上表示标签内有多个类。您可以过滤具有多个类的tr 标签,如下所示:

html_doc = """
<html><head><title>a title here</title></head>
<body>
<tr class="admin-bookings-table-row bookings-history-row  paid   " id="link1">Elsie</tr>,
<tr class="oneclass" id="link2">Lacie</tr>
<tr class="tag1 tag2" id="link3">Tillie</tr>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
filt = [tag for tag in soup.find_all('tr') if len(tag.get('class')) > 1]

filt  # Only 2 of 3 tags returned--excludes tag with just 1 class
# [<tr class="admin-bookings-table-row bookings-history-row paid " id="link1">Elsie</tr>,
#  <tr class="tag1 tag2" id="link3">Tillie</tr>]

或者,使用 lambda:

soup.find_all(lambda tag: tag.name=='tr' and len(tag.get('class')) > 1)

【讨论】:

  • "with a given class" 不确定 OP 是否正在寻找所有多个类
  • 好的,我明白你的意思了。 @RuBiCK,我误解了吗?
  • 我希望能够使用正则表达式并处理字符串 :) 在这种情况下的目标是通过 "admin-bookings-table-row bookings-history-rowpaid" 找到,只有三个类同时
  • @RuBiCK 在这种情况下,不幸的是,我对您在这里要做什么感到更加困惑。如果您想同时查找“所有类”,那么您可以直接使用find_all,而无需指定任何属性。
猜你喜欢
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 2022-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多