Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用空间美化群和类_Python_Beautifulsoup - Fatal编程技术网

Python 用空间美化群和类

Python 用空间美化群和类,python,beautifulsoup,Python,Beautifulsoup,使用BeautifulSoul和Python,我想查找\u all所有与给定类属性匹配的tr项,该类属性包含多个名称,如下所示: <tr class="admin-bookings-table-row bookings-history-row paid "> 我试过几种方法来匹配那个班。正则表达式,通配符,但我总是得到一个空列表 是否有任何方法可以使用正则表达式、通配符或如何匹配此类 有相同的问题没有答案。HTML类不能包含空格。此元素有多个类 通过以下任一类进行搜索都有

使用BeautifulSoul和Python,我想
查找\u all
所有与给定类属性匹配的
tr
项,该类属性包含多个名称,如下所示:

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

我试过几种方法来匹配那个班。正则表达式,通配符,但我总是得到一个空列表

是否有任何方法可以使用正则表达式、通配符或如何匹配此类


有相同的问题没有答案。

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'}))
从bs4导入美化组
html=“”
soup=BeautifulSoup(html,'html.parser')
打印(soup.find_all(attrs={'class':'admin bookings table row'}))
打印(soup.find_all(attrs={'class':'bookings history row'}))
打印(soup.find_all(attrs={'class':'paid'}))
全部输出

[<tr class="admin-bookings-table-row bookings-history-row paid " 
 id="history_row_938220" style=""></tr>]
[]
我想
查找\u all
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>]
可以使用来匹配多个类:

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>]
从bs4导入BeautifulSoup作为汤
html=“”
'''
汤=汤(html,“lxml”)
res=soup.select('tr.admin-bookings-table-row.bookings history row.paid')
打印(res)
>>> []

否则,也许这个答案也能帮助你:

根据记录,类中不能有空格。此元素有多个类。问题似乎是查找所有具有多个类的
tr
项。我不确定这是否会找到元素。我刚刚演示了使用这3个类中的任何一个可以找到元素,所以我不确定OP是如何得到一个空列表的。这不是问题。“我想找到包含多个空格的给定类的所有tr项。”如果你有一个标记
class=“paid”
,你的
attrs
过滤器将返回它,即使它只有一个类。@bradsomon现在我们进入语义
“我想找到一个包含多个空格的给定类的所有tr项。”
从定义上讲是错误的(而且是不可能的),因为没有
“一个包含多个空格的给定类”
。Beautiful Soup在按类搜索时使用包含逻辑(通过传递类列表可以实现上述相同的行为:
Soup.find_all(attrs={'class':['admin-bookings-table-row','bookings history row','paid']})
)Ha。我知道这一点,但其意图显然是将多个空间等同于多个类。让我们试着澄清一下,“对于给定的类”不确定OP是在寻找所有多个类OK,我明白你的意思@RuBiCK,我误解了吗?我希望能够使用正则表达式并处理一个字符串:)在这种情况下,目标是通过“admin bookings table row bookings history row paid”找到,同时只有三个类time@RuBiCK不幸的是,在这种情况下,我对你在这里要做的事情更加困惑。如果您想“同时查找所有类”,那么您可以使用
find_all
,而无需任何属性规范。有趣的方法。css选择器是否可以返回第一个匹配项,如
soup.find()
?Ofc,您可以尝试使用
select\u one
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>]