Convert a HTML Table to JSON

后端 未结 1 1038
臣服心动
臣服心动 2020-11-29 06:01

I\'m trying to convert a table I have extracted via BeautifulSoup into JSON.

So far I\'ve managed to isolate all the rows, though I\'m not sure how to work with the

1条回答
  •  暖寄归人
    2020-11-29 06:59

    Probably your data is something like:

    html_data = """
    
    Card balance $18.30
    Card name NAMEn
    Account holder NAME
    Card number 1234
    Status Active
    """

    From which we can get your result as a list using this code:

    from bs4 import BeautifulSoup
    table_data = [[cell.text for cell in row("td")]
                             for row in BeautifulSoup(html_data)("tr")]
    

    To convert the result to JSON, if you don't care about the order:

    import json
    print json.dumps(dict(table_data))
    

    Result:

    {
        "Status": "Active",
        "Card name": "NAMEn",
        "Account holder":
        "NAME", "Card number": "1234",
        "Card balance": "$18.30"
    }
    

    If you need the same order, use this:

    from collections import OrderedDict
    import json
    print json.dumps(OrderedDict(table_data))
    

    Which gives you:

    {
        "Card balance": "$18.30",
        "Card name": "NAMEn",
        "Account holder": "NAME",
        "Card number": "1234",
        "Status": "Active"
    }
    

    0 讨论(0)
提交回复
热议问题