源码

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']
]
(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/python-jianghtmlbiaozhuanhuanweijson.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9月 27, 2019 at 02:53 上午

热评文章