【问题标题】:convert HTML to json in python using BeautifulSoup使用 BeautifulSoup 在 python 中将 HTML 转换为 json
【发布时间】:2018-05-20 11:50:52
【问题描述】:

我正在尝试使用 beautifulsoup() 函数 python 将 HTML 表转换为 json,我能够将其他一些表转换为 json,但这是不同的类型。

我正在尝试实现这样的目标:

[{
    "name": "abc",
    "age": "21",
    "sex": "m",
    "loction": "us",
    "language": "en"
}, {
    "name": "xyz",
    "age": "25",
    "sex": "f",
    "loction": "us",
    "language": "en"
}]

表格是:

<table><colgroup><col /><col /><col /><col /><col /></colgroup>
<tbody>
<tr>
<th><span>name</span></th>
<th><span>age</span></th>
<th><span>sex</span></th>
<th><span>location</span></th>
<th><span>language</span></th>
</tr>
<tr>
<td colspan="1">
<p><span>abc</span></p>
</td>
<td colspan="1"><span>21</span></td>
<td colspan="1"><span>m</span></td>
<td colspan="1">us</td>
<td colspan="1">en</td>
</tr>
<tr>
<td colspan="1">
<p><span>xyz</span></p>
</td>
<td colspan="1"><span>25</span></td>
<td colspan="1">f</td>
<td colspan="1">us</td>
<td colspan="1">en</td>
</tr>
</tbody>
</table>

感谢您的帮助

【问题讨论】:

    标签: python json beautifulsoup


    【解决方案1】:

    当然,您可以手动制作字典列表,但我们也可以通过使用 pandas.read_html() 转换 pandas.DataFrame 来完成,而无需进行 0 显式 HTML 解析:

    from pprint import pprint
    import pandas as pd
    
    data = """your HTML"""
    
    df = pd.read_html(data, flavor="lxml")[0]
    
    new_header = df.iloc[0]
    df = df[1:]
    df.columns = new_header
    
    pprint(df.to_dict('records'))
    

    打印:

    [{'age': '21', 'language': 'en', 'location': 'us', 'name': 'abc', 'sex': 'm'},
     {'age': '25', 'language': 'en', 'location': 'us', 'name': 'xyz', 'sex': 'f'}]
    

    【讨论】:

    • 我在安装 pandas 时遇到问题,它依赖于 numpy,不会出现此错误安装。弃用:卸载 distutils 安装的项目(numpy)已被弃用,并将在未来的版本中删除
    【解决方案2】:

    我编写了一个库来将 HTML 转换为 JSON,它可以做到这一点:html-to-json

    您可以使用pypi 安装库。安装库后,您将运行:

    import html_to_json
    
    s = '''<table><colgroup><col /><col /><col /><col /><col /></colgroup>
    <tbody>
    <tr>
    <th><span>name</span></th>
    <th><span>age</span></th>
    <th><span>sex</span></th>
    <th><span>location</span></th>
    <th><span>language</span></th>
    </tr>
    <tr>
    <td colspan="1">
    <p><span>abc</span></p>
    </td>
    <td colspan="1"><span>21</span></td>
    <td colspan="1"><span>m</span></td>
    <td colspan="1">us</td>
    <td colspan="1">en</td>
    </tr>
    <tr>
    <td colspan="1">
    <p><span>xyz</span></p>
    </td>
    <td colspan="1"><span>25</span></td>
    <td colspan="1">f</td>
    <td colspan="1">us</td>
    <td colspan="1">en</td>
    </tr>
    </tbody>
    </table>'''
    
    
    html_to_json.convert_tables(s)
    

    这将为您提供(name 键的值周围的\n 是因为包含每个名称的&lt;td&gt; 中都有换行符):

    [
      [
        {
          "name": "\nabc\n",
          "age": "21",
          "sex": "m",
          "location": "us",
          "language": "en"
        },
        {
          "name": "\nxyz\n",
          "age": "25",
          "sex": "f",
          "location": "us",
          "language": "en"
        }
      ]
    ]
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 2015-09-09
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多