Я пытаюсь разобрать ячейки 3-го столбца <table>
с помощью Jsoup.
Вот HTML:
<b><table title="Avgångar:" class="tableMenuCell" cellspacing="0" cellpadding="4" border="0" id="GridViewForecasts" style="color:#333333;width:470px;border-collapse:collapse;">
<tr class="darkblue_pane" style="color:White;font-weight:bold;">
<th scope="col">Linje</th>
<th scope="col">Destination</th>
<th scope="col">Nästa tur (min)</th>
<th scope="col"> </th>
<th scope="col">Därefter</th>
<th scope="col"> </th>
</tr>
<tr class="white_pane" style="color:#333333;">
<td align="right" style="color:#000000;background-color:#01AEF0;">1</td>
<td align="left">Hovshaga Kurortsv.</td><td align="right">55</td>
<td align="left"></td>
<td align="right">--</td>
<td align="left"></td>
</tr>
<tr class="lightblue_pane" style="color:#284775;">
<td align="right" style="color:#000000;background-color:#01AEF0;">1</td>
<td align="left">Hovshaga via Resecentrum</td><td align="right">21</td>
<td align="left"></td><td align="right">--</td>
<td align="left"></td>
</tr>
<tr class="white_pane" style="color:#333333;">
<td align="right" style="color:#000000;background-color:#01AEF0;">1</td>
<td align="left">Teleborg</td><td align="right">5</td>
<td align="left"></td><td align="right">45</td><td align="left"></td>
</tr>
</table></b>
Вот моя попытка кода, которая выдает NullPointerException
:
URL url = null;
try {
url = new URL("http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Teleborg+C+(V%C3%A4xj%C3%B6)");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("1");
Document doc = null;
try {
System.out.println("2");
doc = Jsoup.parse(url, 3000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("3");
Element table = doc.select("table[title=Avgångar:]").first();
System.out.println("3");
Iterator<Element> it = table.select("td").iterator();
//we know the third td element is where we wanna start so we call .next twice
it.next();
it.next();
while(it.hasNext()){
// do what ever you want with the td element here
System.out.println("::::::::::"+it.next());
//iterate three times to get to the next td you want. checking after the first
// one to make sure
// we're not at the end of the table.
it.next();
if(!it.hasNext()){
break;
}
it.next();
it.next();
}
Идет до второго System.Out.Println("3");
, а затем застревает.