我想问一下从<table>
解析一个值colspan和rowspan。
例如:
<table cellpadding="2" cellspacing="2" border="1" width="50%">
<tbody>
<tr>
<td valign="top" rowspan="2" colspan="1" align="center">NO<br>
</td>
<td valign="top" rowspan="1" colspan="3" align="center">NAMA<br>
</td>
<td valign="top" rowspan="1" colspan="2" align="center">TELEPON<br>
</td>
<td valign="top" rowspan="2" colspan="1" align="center">KODE<br>
</td>
</tr>
<tr>
<td valign="top" align="center">DEPAN<br>
</td>
<td valign="top" align="center">TENGAH<br>
</td>
<td valign="top" align="center">BELAKANG<br>
</td>
<td valign="top" align="center">KODE<br>
</td>
<td valign="top" align="center">NO TLP<br>
</td>
</tr>
<tr>
<td valign="top" align="center">1<br>
</td>
<td valign="top">Ani<br>
</td>
<td valign="top">Tiara<br>
</td>
<td valign="top">Ramadika<br>
</td>
<td valign="top" align="center">021<br>
</td>
<td valign="top" align="center">8466729<br>
</td>
<td valign="top" align="center">17412<br>
</td>
</tr>
<tr>
<td valign="top" align="center">2<br>
</td>
<td valign="top">Dia<br>
</td>
<td valign="top">Andari<br>
</td>
<td valign="top">Putri<br>
</td>
<td valign="top" align="center">022<br>
</td>
<td valign="top" align="center">5930290<br>
</td>
<td valign="top" align="center">18291<br>
</td>
</tr>
<tr>
<td valign="top" align="center">3<br>
</td>
<td valign="top">Rangga<br>
</td>
<td valign="top">Dimas<br>
</td>
<td valign="top">Putra<br>
</td>
<td valign="top" align="center">023<br>
</td>
<td valign="top" align="center">8349829<br>
</td>
<td valign="top" align="center">13901<br>
</td>
</tr>
<tr>
<td valign="top" align="center">4<br>
</td>
<td valign="top">Niko<br>
</td>
<td valign="top">Reza<br>
</td>
<td valign="top">Anggara<br>
</td>
<td valign="top" align="center">024<br>
</td>
<td valign="top" align="center">4284982<br>
</td>
<td valign="top" align="center">21211<br>
</td>
</tr>
</tbody>
</table>
我使用python进行HTML解析,如下所示:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
t = soup.find("table")
dat = [ map(str, row.findAll('td', { 'rowspan' })) for row in t.findAll("tr") ]
print dat[1]
但是,我真的很困惑如何获得colspan
的价值。
我已经有了解析标记表,但我不知道如何获取colspan
属性的值。我试过使用正则表达式,但我没有成功。
答案 0 :(得分:1)
我建议使用CSS selectors:
from bs4 import BeautifulSoup
s = open("colspan_rowspan.html").read()
soup = BeautifulSoup(s)
# select all td children of tr that have both colspan & rowspan
tags = soup.select('tr td[colspan,rowspan]')
# print out the values, for example:
print [(td['colspan'], td['rowspan']) for tags]
# will return [('1', '2'), ('3', '1'), ('2', '1'), ('1', '2')]
答案 1 :(得分:-2)
使用colspan和rowspan解析表的最简单方法是将nodejs与cheerio和cheerio-tableparser插件一起使用。
var cheerio = require('cheerio'),
cheerioTableparser = require('cheerio-tableparser'),
fs = require('fs');
var text = fs.readFileSync("colspan_rowspan.html", 'utf8');
$ = cheerio.load(text);
cheerioTableparser($);
data = $("table").parsetable(true, true, true);
console.log(data);
//data = >
//[ [ 'NO', 'NO', '1', '2', '3', '4' ],
// [ 'NAMA', 'DEPAN', 'Ani', 'Dia', 'Rangga', 'Niko' ],
// [ 'NAMA', 'TENGAH', 'Tiara', 'Andari', 'Dimas', 'Reza' ],
// [ 'NAMA', 'BELAKANG', 'Ramadika', 'Putri', 'Putra', 'Anggara' ],
// [ 'TELEPON', 'KODE', '021', '022', '023', '024' ],
// [ 'TELEPON', 'NO TLP', '8466729', '5930290', '8349829', '4284982' ],
// [ 'KODE', 'KODE', '17412', '18291', '13901', '21211' ] ]
结果将是列数组。所有由colspan和rowspan单元合并的数组都将具有相同的值。