如何使用Jsoup选择元素

时间:2014-02-18 09:18:26

标签: java jsoup

我有一个表格,我希望迭代根tbody中的行(tr)而不是其中的行子

我试试这个但没有成功:

Elements tableRows = table.select("tr");

这也得到了孩子所以我有重复

Elements tableRows = table.select("tbody > td:first-child");

这也不起作用

这是我的表:

<table border="0" cellspacing="0" cellpadding="2" width="100%" align="center">
 <tbody>
  <tr>
   <td class="head">Rank</td>
   <td class="Head">&nbsp;</td>
   <td class="Lhead">Club</td>
   <td class="head">Nation</td>
   <td class="head">Rank points</td>
   <td class="head">Five best results (by points)</td>
  </tr>
  <tr>
   <td class="playerB">60. (1.)</td>
   <td class="Lplayer"><a href="player.aspx?id=260161">Imrich Blasko</a></td>
   <td class="Lplayer"><a href="club.aspx?id=260008">THC Blue Dragon Kosice</a></td>
   <td class="player"><img src="flags/SVK.gif" alt="SVK" /></td>
   <td class="playerB">3&nbsp;398</td>
   <td class="Lplayer">
    <table border="0" width="100%" cellspacing="0" cellpadding="0">
     <tbody>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=14379">World Championships 2013</a></td>
       <td class="Rplayer">817</td>
      </tr>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=14192">Czech Open 2012</a></td>
       <td class="Rplayer">674</td>
      </tr>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=15844">Czech Open 2013</a></td>
       <td class="Rplayer">668</td>
      </tr>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=16982">Slovak Championships 2013</a></td>
       <td class="Rplayer">621</td>
      </tr>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=17781">Bratislava Cup 2013</a></td>
       <td class="Rplayer">617</td>
      </tr>
     </tbody>
    </table></td>
  </tr>
  <tr>
   <td colspan="6">
    <hr color="#008080" width="100%" /></td>
  </tr>
  <tr>
   <td class="playerB">159. (2.)</td>
   <td class="Lplayer"><a href="player.aspx?id=650633">Tibor Valachovic ml.</a></td>
   <td class="Lplayer"><a href="club.aspx?id=260007">SHK Skalica</a></td>
   <td class="player"><img src="flags/SVK.gif" alt="SVK" /></td>
   <td class="playerB">2&nbsp;847</td>
   <td class="Lplayer">
    <table border="0" width="100%" cellspacing="0" cellpadding="0">
     <tbody>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=15844">Czech Open 2013</a></td>
       <td class="Rplayer">639</td>
      </tr>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=16318">Brno 2013</a></td>
       <td class="Rplayer">594</td>
      </tr>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=17784">Brno 2014</a></td>
       <td class="Rplayer">583</td>
      </tr>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=16982">Slovak Championships 2013</a></td>
       <td class="Rplayer">569</td>
      </tr>
      <tr>
       <td class="Lplayer"><a href="tournament.aspx?id=12576">Czech Open 2011</a></td>
       <td class="Rplayer">461</td>
      </tr>
     </tbody>
    </table></td>
  </tr>
  <tr>
   <td colspan="6">
    <hr color="#008080" width="100%" /></td>
  </tr>
 </tbody>
</table>

1 个答案:

答案 0 :(得分:2)

尝试:

Elements tableRows = table.select(":root > tbody > tr");