如何解析表的单个列?

时间:2013-11-30 08:57:29

标签: jsoup

我是android编程的初学者。我正在开发一个使用jsoup解析来自网站的HTML表数据的应用程序。我面临的问题是,表没有id,所有表数据都相同class name。我如何从单个列中选择数据?我只想要第二列的表数据

这是HTML:

<table  width=100% border=0 cellpadding=1 cellspacing=1>

  <tr class="rowdeep">

    <td align="center" class="bodytext"><strong>Train No</strong></td>
    <td align="center" class="bodytext"><strong>Name</strong></td>
    <td align="center" class="bodytext"><strong>Off Day</strong></td>
    <td align="center" class="bodytext"><strong>From</strong></td>
    <td align="center" class="bodytext"><strong>Departure</strong></td>
    <td align="center" class="bodytext"><strong>To</strong></td>
    <td align="center" class="bodytext"><strong>Arrival</strong></td>

  </tr>

  <tr class="rowlight">

    <td align="center" class="bodytext">773</td>
    <td align="center" class="bodytext">Kalani Express</td>
    <td align="center" class="bodytext">Friday</td>
    <td align="center" class="bodytext">Dhaka</td>
    <td align="center" class="bodytext">4:00:00 PM</td>
    <td align="center" class="bodytext">Sylhet</td>
    <td align="center" class="bodytext">10:45:00 PM</td>

  </tr>

  <tr class="rowlight">
    <td align="center" class="bodytext">707</td>
    <td align="center" class="bodytext">Tista Express</td>
    <td align="center" class="bodytext">Monday</td>
    <td align="center" class="bodytext">Dhaka</td>
    <td align="center" class="bodytext">7:20:00 AM</td>
    <td align="center" class="bodytext">Dewangonge Bazar</td>
    <td align="center" class="bodytext">12:55:00 PM</td>
 </tr>

<table>

1 个答案:

答案 0 :(得分:1)

因此,假设您已经将HTML解析为文档,这应该可行:

Elements tableRows = doc.select("tr.rowdeep, tr.rowlight");

for (Element e : tableRows) {
    System.out.println(e.child(1).text()); // index of 2nd column
}

将输出:

Name
Kalani Express
Tista Express