gpt4 book ai didi

python - 将嵌套的 html 表转换为 python 中的嵌套字典?

转载 作者:行者123 更新时间:2023-12-05 07:23:46 24 4
gpt4 key购买 nike

我正在编写一个应用程序,将从网站(通过调用 RESR API)接收的 html 表字符串数据转换为字典格式。问题是 HTML 表格字符串的格式是嵌套的 HTML 表格格式。在互联网上搜索了一段时间后,我找不到这种情况的解决方案。尽管它有很多将 json 转换为 html 的解决方案。我的 HTML 表格字符串输入是:

<table>
<tr>
<td>
<table>
<tr>
<th>sku</th>
<td>
<table>
<tr>
<th>capacity</th>
<td>1</td>
</tr>
<tr>
<th>name</th>
<td>Developer</td>
</tr>
</table>
</td>
</tr>
<tr>
<th>tags</th>
<td></td>
</tr>
<tr>
<th>properties</th>
<td>
<table>
<tr>
<th>gatewayRegionalUrl</th>
<td>https:test</td>
</tr>
<tr>
<th>createdAtUtc</th>
<td>2019-03-18T08:11:21.0001331Z</td>
</tr>
<tr>
<th>virtualNetworkType</th>
<td>None</td>
</tr>
<tr>
<th>additionalLocations</th>
<td>None</td>
</tr>
<tr>
<th>customProperties</th>
<td>
<table>
<tr>
<th>Protocols.Server.Http2</th>
<td>False</td>
</tr>
</table>
</td>
</tr>
<tr>
<th>certificates</th>
<td>None</td>
</tr>
</table>
</td>
</tr>
<tr>
<th>etag</th>
<td>AAAAAAFy3Vo=</td>
</tr>
<tr>
<th>type</th>
<td>test/service</td>
</tr>
<tr>
<th>id</th>
<td>/test</td>
</tr>
</table>
</td>
</tr>
</table>

我使用 python 库 BeautifulSoup 通过方法 find_all() 处理 HTML 字符串以查找所有表标签并提取键和值的 th 和 td 标签但问题是我如何处理另一个表标签内的子表标签.我考虑过使用 BeautifulSoup 库的递归函数来解决这个问题,有人建议我怎么做吗?

import json
from bs4 import BeautifulSoup

str_html = "<table><tr><td><table><tr><th>sku</th><td><table><tr><th>capacity</th><td>1</td></tr><tr><th>name</th><td>Developer</td></tr></table></td></tr><tr><th>tags</th><td></td></tr><tr><th>properties</th><td><table><tr><th>gatewayRegionalUrl</th><td>https:test/td></tr><tr><th>createdAtUtc</th><td>2019-03-18T08:11:21.0001331Z</td></tr><tr><th>virtualNetworkType</th><td>None</td></tr><tr><th>additionalLocations</th><td>None</td></tr><tr><th>customProperties</th><td><table><tr><th>Protocols.Server.Http2</th><td>False</td></tr></table></td></tr><tr><th>certificates</th><td>None</td></tr></table></td></tr><tr><th>etag</th><td>AAAAAAFy3Vo=</td></tr><tr><th>type</th><td>test/service</td></tr><tr><th>id</th><td>test</td></tr></table></td></tr></table>"
print(type(str_html))
for table in soup.find_all('table'):
keys = [th.get_text(strip=True)for th in table.find_all('th')]
values = [td.get_text("strip=True) for td in table.find_all('td')]
d = dict(zip(keys, values))
print(d)

My expectation result output is:

             {
"etag": "AAAAAAFy3Vo=",
"id": "test",
"properties": {
"additionalLocations": null,
"certificates": null,
"createdAtUtc": "2019-03-18T08:11:21.0001331Z",
"customProperties": {
"Protocols.Server.Http2": "False",
},
"gatewayRegionalUrl": "https:test",
"virtualNetworkType": "None"
},
"sku": {
"capacity": 1,
"name": "Developer"
},
"tags": {},
"type": "test/service"
}

最佳答案

您可以使用 lxml 库来解析 html 文档,如下所示:

doc = lxml.html.fromstring(page_html)

之后,您可以像这样提取第 th 个 block :

ths = doc.xpath('//th'):

并在每个 th block 中迭代,以相同的方式提取对 (key,value)。最后将数据转成json。

关于python - 将嵌套的 html 表转换为 python 中的嵌套字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55805997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com