【问题标题】:Convert nested html-table to nested-dictionary in python?在python中将嵌套的html表转换为嵌套字典?
【发布时间】:2019-09-12 07:56:33
【问题描述】:

我正在编写一个应用程序,将从网站(通过调用 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 来处理 HTML 字符串,使用方法 find_all() 来查找所有表标记并提取键和值的 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)

我的期望结果输出是:

             {
                "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"
            }

【问题讨论】:

    标签: python dictionary html-table beautifulsoup


    【解决方案1】:

    您可以像这样使用 lxml 库来解析 html 文档:

    doc = lxml.html.fromstring(page_html)
    

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-12-05
      • 2023-03-11
      • 2020-09-03
      • 2020-05-16
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多