2012-01-24 91 views
0

我使用JSOUP從包含玩家名稱和統計信息的網站上的表格中刮取數據。我能夠成功地颳起這個名字,但是當我運行我的迭代時它總是爲每個球員提供相同的統計信息JSOUP問題刮臉表

任何想法?

網站來源:

<tr id="plyr22" class="pncPlayerRow playerTableBgRow0"><td class="playertableData">1</td><td class="playertablePlayerName" id="playername_22" style=""><a href="" class="flexpop" content="tabs#ppc" instance="_ppc" fpopHeight="357px" fpopWidth="490px" tab="null" leagueId="0" playerId="22" teamId="-2147483648" cache="true">Kobe Bryant</a>, LAL&nbsp;SG<a href="" class="flexpop" content="tabs#ppc" instance="_ppc" fpopHeight="357px" fpopWidth="490px" tab="1" leagueId="0" playerId="22" teamId="-2147483648" cache="true"><img src="http://g.espncdn.com/s/fbalm/12/images/icons/sml/news_recent.png" height="12" width="15" border="0" style="margin:0 6px 0 6px" title="Recent News" /></a></td><td class="sectionLeadingSpacer"></td><td class="playertableData">0.63</td><td class="playertableData">2.41</td><td class="playertableData">1.44</td><td class="playertableData">1.29</td><td class="playertableData">2.82</td><td class="playertableData">1.84</td><td class="playertableData">0.18</td><td class="playertableData">4.81</td><td class="playerTableSpacerCell"></td><td class="playertableData sortedCell">15.41</td></tr><tr id="plyr167" class="pncPlayerRow playerTableBgRow1"><td class="playertableData">2</td><td class="playertablePlayerName" id="playername_167" style=""><a href="" class="flexpop" content="tabs#ppc" instance="_ppc" fpopHeight="357px" fpopWidth="490px" tab="null" leagueId="0" playerId="167" teamId="-2147483648" cache="true">Joe Johnson</a>, Atl&nbsp;SG, SF<a href="" class="flexpop" content="tabs#ppc" instance="_ppc" fpopHeight="357px" fpopWidth="490px" tab="2" leagueId="0" playerId="167" teamId="-2147483648" cache="true"><img src="http://g.espncdn.com/s/fbalm/12/images/icons/sml/video_breaking.png" height="11" width="19" border="0" style="margin:0 6px 0 6px" title="Breaking Video" /></a></td><td class="sectionLeadingSpacer"></td><td class="playertableData">-0.62</td><td class="playertableData">1.70</td><td class="playertableData">3.41</td><td class="playertableData">0.41</td><td class="playertableData">1.48</td><td class="playertableData">0.79</td><td class="playertableData">0.04</td><td class="playertableData">2.27</td><td class="playerTableSpacerCell"></td><td class="playertableData sortedCell">9.47</td></tr><tr id="plyr265" class="pncPlayerRow playerTableBgRow0"><td 

代碼:

public static void main(String[] args) throws IOException, SQLException, 
     InterruptedException { 
    Document doc = Jsoup.connect(html).get(); 
    String title = doc.title(); 
    System.out.println(title); 
    int tdCount = 1; 
    String name = null; 
    Double stat1 = null; 
    Double stat2 = null; 
    Double stat3 = null; 
    Double stat4 = null; 
    Double stat5 = null; 
    Double stat6 = null; 
    Double stat7 = null; 
    Double stat8 = null; 
    Double stat9 = null; 
    Double stat10 = null; 

    Iterator<Element> trSIter = doc.select("table.playerTableTable") 
      .iterator(); 
    while (trSIter.hasNext()) { 
     Element trEl = trSIter.next().child(0); 
     Elements tdEls = trEl.children(); 
     Iterator<Element> tdIter = tdEls.select("tr").iterator(); 
     boolean firstRow = true; 
     while (tdIter.hasNext()) { 

      Element tr = (Element) tdIter.next(); 
      if (firstRow) { 
       firstRow = false; 
       continue; 
      } 

      while (tdIter.hasNext()) { 

       System.out.println("============================"); 
       Element tdEl = tdIter.next(); 
       name = tdEl.getElementsByClass("playertablePlayerName") 
         .text(); 
       System.out.println("Name: " + name); 

       // System.out.println(tdEl); 
       Elements tdsEls = tdEl.select("td.playertableData"); 
       Iterator<Element> columnIt = tdsEls.iterator(); 
       boolean firstRow1 = true; 
       while (columnIt.hasNext()) { 

        Element td = (Element) columnIt.next(); 
        if (firstRow1) { 
         firstRow1 = false; 
         continue; 
        } 

        while (columnIt.hasNext()) { 

         Element column = columnIt.next(); 
         switch (tdCount++) { 

         case 1: 
          stat1 = Double.parseDouble(column.text()); 
          break; 
         case 2: 
          stat2 = Double.parseDouble(column.text()); 
          break; 
         case 3: 
          stat3 = Double.parseDouble(column.text()); 
          break; 
         case 4: 
          stat4 = Double.parseDouble(column.text()); 
          break; 
         case 5: 
          stat5 = Double.parseDouble(column.text()); 
          break; 
         case 6: 
          stat6 = Double.parseDouble(column.text()); 
          break; 
         case 7: 
          stat7 = Double.parseDouble(column.text()); 
          break; 
         case 8: 
          stat8 = Double.parseDouble(column.text()); 
          break; 
         case 9: 
          stat9 = Double.parseDouble(column.text()); 
          break; 
         case 10: 
          stat10 = Double.parseDouble(column.text()); 
          break; 
         } 

         // System.out.print(column.className()+":"+column.text()+","); 
         System.out.print("stat1: " + stat1 + " stat2: " 
           + stat2 + " stat3: " + stat3 + " stat4: " 
           + stat4 + " stat5: " + stat5 + " stat6: " 
           + stat6 + " stat7: " + stat7 + " stat8: " 
           + stat8 + " stat9: " + stat9 + " stat10: " 
           + stat10); 
        } 
       } 
       System.out.println(); 

      } 
      System.out.println(); 

     } 
    } 
} 

我的輸出

明星得分 - 免費夢幻籃球 - ESPN

名稱:科比,LAL SG

stat1:2.41 stat2:null stat3:null stat4:null stat5:null stat6:null stat7:null stat8:null stat9:null stat10:nullstat1:2.41 stat2:1.44 stat3:null stat4:null stat5:null stat6:null stat7: null stat8:null stat9:null stat10:nullstat1:2.41 stat2:1.44 stat3:1.29 stat4:null stat5:null stat6:null stat7:null stat8:null stat9:null stat10:nullstat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:null stat6:null stat7:null stat8:null stat9:null stat10:nullstat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:null stat7:null stat8:null stat9:null stat10:nullstat1:2.41 stat2 :1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:null stat8:null stat9:null stat10:nullstat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:null stat9 :null stat10:nullstat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:null stat10:null

名稱:喬 - 約翰遜是ATL SG,SF

STAT1:2.41 STAT2:1.44 STAT3:1.29 STAT4:2.82 STAT5:1.84 STAT6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:nullstat1:2.41 STAT2 :1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5: 1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2 :1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9: 1.7 stat10:3.41

名稱:伊戈達拉,披SF,SG

STAT1:2.41 STAT2:1.44 STAT3:1.29 STAT4:2.82 STAT5:1.84 STAT6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10: 3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7 :4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 sta t3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9: 1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6 :0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3。41

名稱:詹姆斯 - 哈登,雷霆SG

STAT1:STAT2 2.41:1.44 STAT3:1.29 STAT4:2.82 STAT5:1.84 STAT6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 STAT2 :1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5: 1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1: 2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8 :15.41 stat9:1.7 stat10:3.41

名稱:蒙塔 - 埃利斯,GS PG,SG

STAT1:2.41 STAT2:1.44 STAT3:1.29 STAT4:2.82 STAT5:1.84 STAT6:0.18 stat7:4.81 stat8:15.41 stat9 :1.7 stat10 3.41stat1 2.41 stat2 1.44 stat3 1.29 stat4 2.82 stat5 1.84 stat6 0.18 stat7 4.81 stat8 15.41 stat9 1.7 stat10 3.41stat1 2.41 stat2 1.44 stat3 1.29 stat4 2.82 stat5 1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 st at2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8: 15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5 :1.84 STAT6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41

名稱:凱文 - 馬丁,侯SG

STAT1:2.41 STAT2:1.44 STAT3:1.29 STAT4:2.82 STAT5:1.84 STAT6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9: 1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6 :0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41stat1:2.41 stat2:1.44 stat3:1.29 stat4:2.82 stat5:1.84 stat6:0.18 stat7:4.81 stat8:15.41 stat9:1.7 stat10:3.41

回答

0

當您移至下一行時,您忘記了重置tdCount

+0

你是什麼意思重置? – user1163234

+0

另外每個玩家都獲得10次每個stst? – user1163234