【发布时间】:2021-11-18 13:40:03
【问题描述】:
我想知道如何使用 BeautifulSoup 在以下 html 代码中解析这种样式(javascript?)的标记属性:
<div class="class1" data-prop="{personName: 'Claudia', personCode:'123456'}">
...
</div>
我目前只是遵循标准流程,直到我到达我当前正在使用正则表达式解析的属性的内容,但是我想知道是否有更好/更快/更优雅的选项:
soup = BeautifulSoup(data,'html.parser')
class_element = soup.find("div", class_="class1")
data-props=class_element['data-prop']
# Parsing using regexp goes here
【问题讨论】:
-
import json; data = json.loads(data_props)? -
@buran 哦,对不起,现在我明白为什么了
-
是的,这不起作用。我已经试过了。
-
OP 你能指定使用正则表达式解析什么或如何解析吗?您可能对使用正则表达式的另一种解析方式感兴趣?
标签: javascript python html beautifulsoup