Python HTML Table to JSON



我需要一些帮助,尝试在Python中将多个HTML表转换为JSON。我有以下几点:

[<table>n<tr><th nowrap="">FRUIT</th><td>APPLE</td></tr>n<tr><th nowrap="">COLOR</th><td>GREEN</td></tr>n</table>, <table>n<tr><th nowrap="">FRUIT</th><td>BANANA</td></tr>n<tr><th nowrap="">COLOR</th><td>YELLOW</td></tr>n</table>]

我想要实现的是用 JSON 输出它:

[
    {
        "FRUIT": "APPLE", 
        "COLOR": "GREEN"
    }, 
    {
        "FRUIT": "BANANA", 
        "COLOR": "YELLOW"
    }
]
In [49]: 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)
    ...:     
    ...:     
    ...:     
{'FRUIT': 'APPLE', 'COLOR': 'GREEN'}
{'FRUIT': 'BANANA', 'COLOR': 'YELLOW'}

最新更新