姬長信(Redy)

python – 将HTML表转换为JSON


我正在尝试将我通过BeautifulSoup提取的表转换为JSON.

到目前为止,我已设法隔离所有行,但我不确定如何使用此处的数据.任何建议将非常感谢.

[Balance$18.30, 
Card nameName, 
Account holderNAME, 
Card number1234, 
StatusActive]

(为了便于阅读,我打破了线路)

这是我的尝试:

result = []
allrows = table.tbody.findAll('tr')
for row in allrows:
    result.append([])
    allcols = row.findAll('td')
    for col in allcols:
        thestrings = [unicode(s) for s in col.findAll(text=True)]
        thetext = ''.join(thestrings)
        result[-1].append(thetext)

这给了我以下结果:

[
 [u'Card balance', u'$18.30'],
 [u'Card name', u'NAMEn'],
 [u'Account holder', u'NAME'],
 [u'Card number', u'1234'],
 [u'Status', u'Active']
]