小编dec*_*ott的帖子

将HTML表转换为JSON

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

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

[<tr><td><strong>Balance</strong></td><td><strong>$18.30</strong></td></tr>, 
<tr><td>Card name</td><td>Name</td></tr>, 
<tr><td>Account holder</td><td>NAME</td></tr>, 
<tr><td>Card number</td><td>1234</td></tr>, 
<tr><td>Status</td><td>Active</td></tr>]
Run Code Online (Sandbox Code Playgroud)

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

这是我的尝试:

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)
Run Code Online (Sandbox Code Playgroud)

这给了我以下结果:

[
 [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']
]
Run Code Online (Sandbox Code Playgroud)

html python json html-table beautifulsoup

14
推荐指数
1
解决办法
2万
查看次数

标签 统计

beautifulsoup ×1

html ×1

html-table ×1

json ×1

python ×1