BeautifulSoup 和带空格的类 [英] BeautifulSoup and class with spaces
本文介绍了BeautifulSoup 和带空格的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 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?
在这里发布了同样的问题没有答案.
推荐答案
您可以使用 css 选择器 匹配多个类:
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
这篇关于BeautifulSoup 和带空格的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文