簡體   English   中英

JSOUP解析兩個表

[英]JSOUP parsing two table

我已經解析了第一個表,但是我不知道如何解析第二個表。

    <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元素。

您可以實例化Elements類,而不是直接在其上調用“ first()”。 然后,您將可以使用“ get(int)”方法解析所需的表:

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

然后,您將獲得一個元素列表,其中包含第二個表中的所有“ td”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM