1

I need some help trying to convert multiple HTML tables to JSON in Python. I have the follwing:

[<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>]

What I'm trying to achieve is to output it in JSON:

[
    {
        "FRUIT": "APPLE", 
        "COLOR": "GREEN"
    }, 
    {
        "FRUIT": "BANANA", 
        "COLOR": "YELLOW"
    }
]
QEWR
  • 65
  • 1
  • 8

1 Answers1

3
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'}
宏杰李
  • 11,820
  • 2
  • 28
  • 35