【发布时间】:2021-04-01 06:42:49
【问题描述】:
我需要将 HTML 转换为 python 字典列表。示例 HTML 代码:
<div data-axite-container="1" data-axite-uuid="1f0c5634-9ff9-4942-861e-2c7e75d6f2ef" data-axite-id="a4d1a127-0fe8-4281-abd6-fe0653a8b519">
<h4>Question 1</h4>
<p>Some text here too</p> <p>And some text here</p>
<h4>Question 2</h4>
<p>Answer Here</p>
<ul><li>text1</li><li>text2</li>
<h4>Question 3</h4>
<p>Answer Here</p><table>...</table>
<h4>Question 4</h4>
<p>Answer</p>
预期结果:
[
{
"q": "Question 1",
"a": ["Some text here too","And some text here"]
},
{
"q": "Question 2",
"a": ["Answer Here","<ul><li>text1</li><li>text2</li>"]
},
{
"q": "Question 3",
"a": ["Answer Here","<table>...</table>"]
},
{
"q": "Question 4",
"a": ["Answer"]
}]
我们将不胜感激任何帮助。在此先感谢:)
【问题讨论】:
-
docs.python.org/3/library/html.parser.html 该模块定义了一个 HTMLParser 类,作为解析 HTML(超文本标记语言)和 XHTML 格式的文本文件的基础。
标签: python html python-3.x