我想问一下从<table>
. 例如像这样:
<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
属性的值。我试过使用正则表达式,但没有成功。