3

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

4

2 回答 2

1

我推荐使用CSS 选择器

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')]
于 2012-11-07T05:40:16.347 回答
-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 合并的单元格将在数组中具有相同的值。

于 2016-02-03T11:17:04.693 回答