简体   繁体   English

JSOUP解析两个表

[英]JSOUP parsing two table

I have parsed the first table but I don't know how to parse the second 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>

The first table parsed with: 第一个表解析为:

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()){

Can you help me with parsing ONLY the second table? 您能帮我解析第二张桌子吗? I want only td elements. 我只想要td元素。

You can instantiate an Elements class instead on directly invoque "first()" on it. 您可以实例化Elements类,而不是直接在其上调用“ first()”。 Then you will be able to parse the table you wish with the "get(int)" method: 然后,您将可以使用“ get(int)”方法解析所需的表:

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

Then you'll get a list of Elements, containing all the "td" in the second table. 然后,您将获得一个元素列表,其中包含第二个表中的所有“ td”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM