2016-12-17 57 views
1

我想在tr中定位特定的td。如何使用java定位HTML文檔中的特定td

這是我的代碼:

 private void fletch(String name) throws IOException, JSONException { 
      final String iron = "img=2"; 
      final String ui = "img=3"; 
      final String hc = "img=10"; 
      String url = "services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1="; 

      if (name.toLowerCase().indexOf(iron.toLowerCase()) != -1) { 
       url = "http://services.runescape.com/m=hiscore_oldschool_ironman/hiscorepersonal.ws?user1="; 
      }else if(name.toLowerCase().indexOf(ui.toLowerCase()) != -1){ 
       url = "http://services.runescape.com/m=hiscore_oldschool_ultimate/hiscorepersonal.ws?user1="; 
      }else if(name.toLowerCase().indexOf(hc.toLowerCase()) != -1){ 
       url = "http://services.runescape.com/m=hiscore_oldschool_hardcore_ironman/hiscorepersonal.ws?user1="; 
      } 

      String[] parts = name.split(">"); 
      String part2 = parts[1]; 
      String fin = part2.replaceAll("\\s","+"); 
      url+=fin; 

      Document doc = Jsoup.connect(url) 
        .data("query", "Java") 
        .userAgent("Mozilla") 
        .cookie("auth", "token") 
        .timeout(3000) 
        .post(); 

    //core part 
      Element table1 = doc.select("table").first(); 
       String body = table1.toString(); 
       Document docb = Jsoup.parseBodyFragment(body); 
       Element bbd = docb.body(); 
       String hhk = bbd.toString();  

//This is where i dont know how to target the td data.. Tried this (cant check code so came on here): 
    String overall = bbd.getElementsByTag("td").get(4).text(); 

現在,這給了我這樣的HTML代碼:

<table cellpadding="3" cellspacing="0" border=0 style="max-width: 355px;"> 
<tr><td colspan="5" align="center"><b>Personal scores for big kurwaaa</b></td></tr> 
<tr> 
<td colspan="2" style="text-align:left;padding-left:24px;"><b>Skill</b></td><td align="right"><b>Rank</b></td><td align="right"><b>Level</b></td><td align="right"><b>XP</b></td> 
</tr> 
<tr><td width="35"></td><td width="100"></td><td width="75"></td><td width="40"></td><td width="75"></td></tr> 
<tr> 

<td></td> 
<td align="left"><a href="overall.ws?table=0&user=big+kurwaaa"> 
Overall 
</a></td> 
<td align="right">7,430</td> 
<td align="right">466</td> 
<td align="right">6,164,312</td> 

</tr> 
<tr> 
<td align="right"><img class="miniimg" src="http://www.runescape.com/img/rsp777/hiscores/skill_icon_attack1.gif"></td> 
<td align="left"><a href="overall.ws?table=1&user=big+kurwaaa"> 
Attack 
</a></td> 
<td align="right">14,475</td> 
<td align="right">19</td> 
<td align="right">4,304</td> 

</tr> 

我想指定的3 TD與每Tr內的數據。例如:

<td align="right">7,430</td> 
<td align="right">466</td> 
<td align="right">6,164,312</td> 

等等從「整體」​​tr到最後。有沒有辦法以一種簡單的方式做到這一點,讓我可以選擇循環訪問數據並創建JSON/map?

PS:新到Java

+0

你想要每個tr的第3個td?或者從每個tr的第3個td到最後td? –

+0

@AshrafulIslam從第三個到最後一個 – CollapsRanger2

回答

0

如果你想獲得BBD使用getElementsByTag內的所有TR標籤。
它會返回元素,通過它可以通過索引瀏覽所有tr標籤(基於索引0)。如果想跳過前3個tr標籤,只需從索引開始循環:3,對於td標籤就這樣
這裏是演示代碼:

Elements trList = bbd.getElementsByTag("tr"); 

for (int i = 3; i < trList.size(); i++) { 
    System.out.println("----------------- TR START -----------------"); 
    Elements tdList = trList.get(i).getElementsByTag("td"); 
    for (int j = 2; j < tdList.size(); j++) { 
     System.out.println(tdList.get(j)); 
    } 
    System.out.println("------------------ TR END ------------------"); 
} 
+1

謝謝你這個簡單的答案。由於名譽而無法贊成,但非常感謝。 PS。改變了System.out.println(tdList.get(j));到System.out.println(tdList.get(j).text());獲取價值。再一次感謝你 – CollapsRanger2

0
 String url = "yourUrl"; 
     Document doc = Jsoup.connect(url).get(); 
     Element table = doc.select("table[class=tableClass]").first(); 
     Iterator<Element> iterator = table.select("td[align=right]").iterator(); 
     iterator.next();//skip first 
     iterator.next();//skip second 
     System.out.println(iterator.next().text()); 
+0

迭代器 iterator = table.select(「td [align = right]」)。iterator();拋出nullpointerex – CollapsRanger2

+0

@ CollapsRanger2我可以很清楚地看到,td align = right存在如此可取地NPE來自表,所以請確保你把它的類的名稱 –

相關問題