【发布时间】:2021-08-17 03:02:04
【问题描述】:
我有以下 bs4 元素:
from bs4 import BeautifulSoup
html_doc = """
<l2 attribute2="Output"><s3><Cell cell_value2="384.01"/></s3></l2>,
<l1><s3 attribute1="Cost"><s4><Cell cell_value1="2314.37"/></s4></s3></l1>
"""
soup = BeautifulSoup(html_doc, "html.parser")
我想像这样提取所有属性值:
["Output", "Cost"]
我的问题是:如何使用正则表达式re.compile(r'^attribute[0-9]$') 实现这一点,并且在attribute* 可以是第一个标签(例如l1 或l2)或者它可以是“更深”的情况下" 比如s3 或者其他任意深度)?
如果属性具有相同的名称,或者它们处于相同的深度级别但名称不同,我可以这样做 - 但不能两者兼而有之。
【问题讨论】:
标签: python regex xml beautifulsoup