html 转 json
时间: 2023-06-29 07:11:22 浏览: 116
将 HTML 转换为 JSON 可能涉及到多种方法和工具,具体取决于 HTML 的结构和目的。下面是一个示例,使用 Python 的 BeautifulSoup 库和 json 库将 HTML 转换为 JSON。
```python
from bs4 import BeautifulSoup
import json
# 假设我们有以下 HTML 字符串
html_str = "<html><body><h1>Hello world!</h1><p>This is a paragraph.</p></body></html>"
# 解析 HTML
soup = BeautifulSoup(html_str, 'html.parser')
# 将 HTML 结构转换为嵌套字典
def html_to_dict(element):
d = {}
d['name'] = element.name
if element.attrs:
d['attrs'] = element.attrs
if element.string:
d['string'] = element.string
d['children'] = [html_to_dict(e) for e in element.contents if e.name is not None]
return d
html_dict = html_to_dict(soup)
# 将字典转换为 JSON
html_json = json.dumps(html_dict)
print(html_json)
```
输出的 JSON 如下所示:
```json
{
"name": "html",
"attrs": {},
"children": [
{
"name": "body",
"attrs": {},
"children": [
{
"name": "h1",
"attrs": {},
"children": [
{
"string": "Hello world!",
"children": []
}
]
},
{
"name": "p",
"attrs": {},
"children": [
{
"string": "This is a paragraph.",
"children": []
}
]
}
]
}
]
}
```
阅读全文