JSOUP解析两个表

时间:2014-04-17 08:22:00

标签: java android parsing jsoup

我已经解析了第一个表,但我不知道如何解析第二个表。

    <div class="catItemHeader" style="position: absolute;">
    <h3 class="catItemTitle" style="color: #982C37;font-size: 30px;font-weight: 600;">Arrivi</h3>

    <div style="color: #982C37; margin-left: 104px;margin-top: -20px;font-weight: bold;">
        <label>Ultimo aggiornamento:</label> 17/04/2014 10:12   </div>
    <table class="tabella-voli">
        <thead>
            <th>Compagnia</th>
            <th>N.</th>
            <th>Provenienza</th>
            <th>Schedulato</th>
            <th>Stimato</th>
            <th>Stato</th>
        </thead>
        <tbody>
                        <tr style="background-color: rgba(253, 253, 253, 0.8);">
                <td>RYANAIR</td>
                <td>03071</td>
                <td>Londra Stansted</td>
                <td>17/04/2014 12:40</td>
                <td>17/04/2014 12:32</td>
                <td>
                                        <img src="/images/volo_green.gif"  alt="In orario" title="In orario"/><br /> In orario              </td>
            </tr>
                        <tr style="background-color: rgba(253, 253, 253, 0.8);">
                <td>RYANAIR</td>
                <td>04075</td>
                <td>Kaunas</td>
                <td>17/04/2014 16:10</td>
                <td>17/04/2014 16:10</td>
                <td>
                                        <img src="/images/volo_green.gif"  alt="In orario" title="In orario"/><br /> In orario              </td>
            </tr>
                        <tr style="background-color: rgba(253, 253, 253, 0.8);">
                <td>RYANAIR</td>
                <td>07316</td>
                <td>Dublino</td>
                <td>17/04/2014 20:45</td>
                <td>17/04/2014 20:45</td>
                <td>
                                        <img src="/images/volo_green.gif"  alt="In orario" title="In orario"/><br /> In orario              </td>
            </tr>
                    </tbody>
    </table>
</div>


<div class="catItemHeader" style="margin-left: 438px;">
    <h3 class="catItemTitle" style="color: #982C37;font-size: 30px;font-weight: 600;">Partenze</h3>

    <div style="color: #982C37;margin-left: 158px;margin-top: -20px;font-weight: bold;">
        <label>Ultimo aggiornamento:</label> 17/04/2014 10:12   </div>
    <table class="tabella-voli">
        <thead>
            <th>Compagnia</th>
            <th>N.</th>
            <th>Destinazione</th>
            <th>Schedulato</th>
            <th>Stimato</th>
            <th>Stato</th>
        </thead>
        <tbody>
                        <tr style="background-color: rgba(253, 253, 253, 0.8);">
                <td>RYANAIR</td>
                <td>03074</td>
                <td>Londra Stansted</td>
                <td>17/04/2014 13:05</td>
                <td>17/04/2014 13:05</td>
                <td>
                                        <img src="/images/volo_green.gif" alt="In orario" title="In orario"/><br /> In orario               </td>
            </tr>
                        <tr style="background-color: rgba(253, 253, 253, 0.8);">
                <td>RYANAIR</td>
                <td>04076</td>
                <td>Kaunas</td>
                <td>17/04/2014 16:35</td>
                <td>17/04/2014 16:35</td>
                <td>
                                        <img src="/images/volo_green.gif" alt="In orario" title="In orario"/><br /> In orario               </td>
            </tr>
                        <tr style="background-color: rgba(253, 253, 253, 0.8);">
                <td>RYANAIR</td>
                <td>07317</td>
                <td>Dublino</td>
                <td>17/04/2014 21:10</td>
                <td>17/04/2014 21:10</td>
                <td>
                                        <img src="/images/volo_green.gif" alt="In orario" title="In orario"/><br /> In orario               </td>
            </tr>
                    </tbody>
    </table>
</div>

 </div>
            </div>
                </div>

</div>

第一个表解析为:

org.jsoup.nodes.Document doc = Jsoup.connect("http://site.eu").timeout(7*1000).get();

org.jsoup.nodes.Element tabella = doc.getElementsByClass("tabella-voli").first();
Iterator<org.jsoup.nodes.Element> iterator = tabella.select("td").iterator();

while(iterator.hasNext()){

你能帮我解析第二张桌子吗?我只想要td元素。

1 个答案:

答案 0 :(得分:0)

您可以直接调用invoque&#34; first()&#34;来实例化一个Elements类。在上面。 然后你就可以用&#34; get(int)&#34;解析你想要的表了。方法:

Elements tables = currentServerPage.getElementsByClass("tabella-voli");
Element secondTable = tables.get(1);
Elements tdElements = currentServerPage.getElementsByTag("td");

然后,您将获得一个元素列表,其中包含所有&#34; td&#34;在第二个表格中。