JSoup:如何在没有类的情况下访问单元格

时间:2015-09-24 21:30:13

标签: java html jsoup

我试图将HTML文件中的数据提取到我的java代码中。当我的单元格有一个类名时,一切顺利。是否有可能获得下一个细胞"或类似的东西?这是HTML和我的代码的一部分。

<table border="1">
<tr>
<th>Termin</th>
<th>Dzień, godzina</th>
<th>Przedmiot</th>
<th>Typ</th>
<th>Nauczyciel</th>
<th>Sala</th>
</tr>
<tr class="">
<td rowspan="2" class="termin">2015-09-30</td>
<td rowspan="2" class="dzien">Śr 07:50 - 09:30</td>
<td>Język obcy</td>
<td>ćwiczenia</td>
<td></td>
<td></td>
</tr>
public class FetchData {
FetchData() {

    try {
        Document doc = Jsoup.connect("http://planzajec.uek.krakow.pl/index.php?typ=G&id=84721&okres=1").get();
        for (Element table : doc.select("table")) {
            for (Element row : table.select("tr")) {

                Elements termin = row.select("td.termin");
                Elements dzien = row.select("td.dzien");
                Elements przedmiot = row.select("td");

                System.out.println(termin.text() + " " + dzien.text());
                System.out.println(przedmiot.text());

            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

3 个答案:

答案 0 :(得分:1)

您可以使用标签检索它们。使用getElementsByTag(yourTag)检索具有指定标记的所有单元格,然后使用for循环遍历它们。

答案 1 :(得分:1)

要选择没有类的元素,您可以使用它:

Elements tds = row.select("td:not([class])");

这使用How to control docking order in WinForms

答案 2 :(得分:1)

Elements elmnts= doc.select("table tr"); 

for(Element e:elmnts){
   System.out.println(e.select("td:eq(0)").text());
   System.out.println(e.select("td:eq(1)").text());
   System.out.println(e.select("td:eq(2)").text());
   System.out.println(e.select("td:eq(3)").text());
   System.out.println(e.select("td:eq(4)").text());
   System.out.println(e.select("td:eq(5)").text());
}

因为您已经提到了我为同一个

提供的示例数据中的6列