2014-04-17 71 views
0

我已经解析了第一个表,但我不知道如何解析第二个表。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元素。

回答

0

您可以直接invoque“first()”来实例化一个Elements类。 然后,你将能够解析您希望与“获取(INT)”的方法表:

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

然后你会得到元素的列表,包含所有“TD”第二个表。

+0

是的,谢谢你......但使用迭代器? – roxdragon

+0

如果你想在“td”列表上使用Iterator,那么你只需要做和以前一样的事情:“Iterator iterator = tdElements.iterator();”,even如果我不明白你为什么绝对需要它:有很多方法来迭代集合... –

+0

迭代器是很好的,为什么我用一段时间()来到最后..其他解决方案? – roxdragon