BeautifulSoup和带有空格的类 [英] BeautifulSoup and class with spaces
问题描述
使用BeautifulSoul和Python,我想find_all
匹配与给定类属性匹配的所有tr
项目,该给定类属性包含多个这样的名称:
With BeautifulSoul and Python, I want to find_all
all the tr
items matching a given class attribute that contains multiple names like this one:
<tr class="admin-bookings-table-row bookings-history-row paid ">
我尝试了几种匹配该类的方法.正则表达式,通配符,但我总是得到一个空列表.
I have tried several ways to match that class. Regular expressions, wildcards but I always get an empty list.
是否可以使用正则表达式,通配符或如何匹配此类?
Is there any way to use regular expressions, wildcards or how to match this class?
在此处发布了相同的问题没有答案.
推荐答案
you can use a css selector to match many classes :
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
Otherwise, maybe this answer can help you too : https://stackoverflow.com/a/46719501/6655211
这篇关于BeautifulSoup和带有空格的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!