将嵌套的HTML表格转换为Python中的嵌套字典?

3

我正在编写一个应用程序,将从网站(通过调用REST 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处理HTML字符串,使用find_all()方法查找所有的table标签,并提取th和td标签作为键值对。但问题在于如何处理另一个table标签内部的子表格标签。我考虑使用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)

我的期望结果输出是:
             {
                "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"
            }
1个回答

0
你可以使用lxml库来解析HTML文档,代码如下:
doc = lxml.html.fromstring(page_html)

然后,您可以像这样提取th块:

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

在每个th块中迭代以以相同的方式提取键值对(key,value)。最后将数据转换为json格式。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接