【问题标题】:How to parse the cells of the 3rd column of a table?如何解析表格第三列的单元格?
【发布时间】:2011-12-13 10:43:36
【问题描述】:

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

这里是 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"); 然后卡住了。

【问题讨论】:

  • "直到 System.Out.Println("3");"哪一个?你有两个……
  • 每个it.next() 调用都不应在没有保护性if(it.hasNext) 测试的情况下进行。除非 it.next() 抛出异常,否则您可以捕获(并且捕获它)。
  • @Thilo:一直到最后一个 system.out.println("3")。
  • 在这种情况下我是否必须放一些正则表达式或者我该怎么办??

标签: java android html-parsing jsoup


【解决方案1】:

这种方法非常混乱,您没有说明 NPE 发生在哪一行,因此很难直接回答您的问题。

除此之外,我建议不要以困难且容易出错的方式进行操作。因为&lt;table&gt; 已经有一个id 属性,它应该在整个文档中是唯一的,所以只需使用ID 选择器#someid。此外,您可以使用索引选择器:eq(index) 获取第三列的单元格(注意:它是从零开始的!)。

所以,那几行简单的就应该做到:

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 是围绕它构建的。

另见:

【讨论】:

    【解决方案2】:

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

    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());
    

    希望它会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多