HtmlUnit学习笔记(二)-----表格的获取使用

官方文档:http://htmlunit.sourceforge.net/table-howto.html

The first set of examples will use this simple html.

简单的测试页面

<html><head><title>Table sample</title></head><body>
    <table id="table1">
        <tr>
            <th>Number</th>
            <th>Description</th>
        </tr>
        <tr>
            <td>5</td>
            <td>Bicycle</td>
        </tr>
    </table>
</body></html>

This example shows how to iterate over all the rows and cells

这个示例演示如何遍历表格的行列数据

final HtmlTable table = page.getHtmlElementById("table1");
for (final HtmlTableRow row : table.getRows()) {
    System.out.println("Found row");
    for (final HtmlTableCell cell : row.getCells()) {
        System.out.println("   Found cell: " + cell.asText());
    }
}

The following sample shows how to access specific cells by row and column

下面的例子演示如何通过通过行、列坐标获取一个指定的数据(getCellAt(row,column))

final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://foo.com");

final HtmlTable table = page.getHtmlElementById("table1");
System.out.println("Cell (1,2)=" + table.getCellAt(1,2));

More complex table

更多复制的table

The next examples will use a more complicated table that includes table header, footer and body sections as well as a caption

<html><head><title>Table sample</title></head><body>
    <table id="table1">
        <caption>My complex table</caption>
        <thead>
            <tr>
                <th>Number</th>
                <th>Description</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td>7</td>
                <td></td>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>5</td>
                <td>Bicycle</td>
            </tr>
        </tbody>
        <tbody>
            <tr>
                <td>2</td>
                <td>Tricycle</td>
            </tr>
        </tbody>
    </table>
</body></html>

HtmlTableHeader, HtmlTableFooter and HtmlTableBody sections are groupings of rows. There can be at most one header and one footer but there may be more than one body. Each one of these contains rows which can be accessed via getRows()

final HtmlTableHeader header = table.getHeader();
final List<HtmlTableRow> headerRows = header.getRows();

final HtmlTableFooter footer = table.getFooter();
final List<HtmlTableRow> footerRows = footer.getRows();

for (final HtmlTableBody body : table.getBodies()) {
    final List<HtmlTableRow> rows = body.getRows();
    ...
}

Every table may optionally have a caption element which describes it.

final String caption = table.getCaptionText()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值