I am using JSOUP to scrape data from a table on a site that contains a name of the player and stats. I am able to scrape the name with success but when I run my iterato rthru th erow It is always picking up the same stats for each player
Any idea?
Site source:
<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 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 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
Code:
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();
}
}
}
My OutPut
Player Rater – Free Fantasy Basketball – ESPN
Name: Kobe Bryant, 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
Name: Joe Johnson, 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
Name: Andre Iguodala, Phi 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 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
Name: James Harden, OKC 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
Name: Monta Ellis, 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 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
Name: Kevin Martin, Hou SG
You forgot to reset
tdCountwhen moving on to next row.