Convert HTML table with a header to Json - Python

后端 未结 2 1570
日久生厌
日久生厌 2021-01-22 09:31

Suppose I have the following HTML table:

2条回答
  •  野的像风
    2021-01-22 10:12

    This code does exactly what you want

    from bs4 import BeautifulSoup
    import json
    
    xml_data = """
    [[your xml data]]"""
    
    
    if __name__ == '__main__':
        model = BeautifulSoup(xml_data, features='lxml')
        fields = []
        table_data = []
        for tr in model.table.find_all('tr', recursive=False):
            for th in tr.find_all('th', recursive=False):
                fields.append(th.text)
        for tr in model.table.find_all('tr', recursive=False):
            datum = {}
            for i, td in enumerate(tr.find_all('td', recursive=False)):
                datum[fields[i]] = td.text
            if datum:
                table_data.append(datum)
    
        print(json.dumps(table_data, indent=4))
    

提交回复
热议问题
Name Age License