如何解析表格第3列的单元格?

时间:2011-10-23 04:55:59

标签: java android html-parsing jsoup

我正在尝试使用Jsoup解析<table>的第3列的单元格。

这是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">&nbsp;</th>
        <th scope="col">Därefter</th>
        <th scope="col">&nbsp;</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");然后它才会卡住。

2 个答案:

答案 0 :(得分:4)

这种方法非常混乱,你没有说出NPE发生在哪一行,所以你很难直截了当地回答你的问题。

除此之外,我建议不要这么做而且容易出错。由于<table>已经有一个id属性,该属性在整个文档中应该是唯一的,因此只需使用ID选择器#someid。此外,您可以使用索引选择器:eq(index)获取第3列的单元格(注意:它基于零!)。

所以,那些简单的行应该这样做:

Document document = Jsoup.connect("http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Teleborg+C+(V%C3%A4xj%C3%B6)").get();
Elements nextTurns = document.select("#GridViewForecasts td:eq(2)");

for (Element nextTurn : nextTurns) {
    System.out.println(nextTurn.text());
}

结果如下:

50
30
10
18
3
24

就是这样。

我强烈建议花一些时间来正确学习CSS选择器语法,因为Jsoup是围绕它构建的。

另见:

答案 1 :(得分:0)

我认为最好的解决方案是使用get();方法从element的数量中获取单个elements

Document doc = Jsoup.connect("your_url").get();
Elements table = doc .getElementById("id_of_your_table");
Element tr = table.select("tr").get(2); // this will get 3rd tr
//if you need 3rd column of 3rd row then 
Element 3rdtd = tr.select("td").get(2);
Log.i("3rd Column of 3rd Row", 3rdtd.text().toString());

希望它会有所帮助。