2012-11-24 116 views
2

我想用JSoup框架提取此表以將內容保存到「表」數組中。第一個tr標籤是表頭。所有以下內容(未包含)描述內容。從JSoup中提取數據表

<table style=h2 width=100% cellspacing="0" cellpadding="4" border="1" bgColor="#FFFFFF"> 
<tr> 
<td align="left" bgcolor="#9999FF" > 
<!-- 0 --> 
Kl. 
</td> 
<td align="left" bgcolor="#9999FF" > 
<!-- 3 --> 
Std. 
</td> 
<td align="left" bgcolor="#9999FF" > 
<!-- 4 --> 
Lehrer 
</td> 
<td align="left" bgcolor="#9999FF" > 
<!-- 5 --> 
Fach 
</td> 
<td align="left" bgcolor="#9999FF" > 
<!-- 6 --> 
Raum 
</td> 
<td align="left" bgcolor="#9999FF" > 
<!-- 7 --> 
VLehrer 
</td> 
<td align="left" bgcolor="#9999FF" > 
<!-- 8 --> 
VFach 
</td> 
<td align="left" bgcolor="#9999FF" > 
<!-- 9 --> 
VRaum 
</td> 
<td align="left" bgcolor="#9999FF" > 
<!-- 13 --> 
Info 
</td> 
</tr> 
<tr> 
<!-- 1 0 --> 
<td align="left" bgcolor="#FFFFFF" > 
&nbsp; 
</td> 
<!-- 1 3 --> 
<td align="left" bgcolor="#FFFFFF" > 
4 
</td> 
<!-- 1 4 --> 
<td align="left" bgcolor="#FFFFFF" > 
Méta 
</td> 
<!-- 1 5 --> 
<td align="left" bgcolor="#FFFFFF" > 
HU 
</td> 
<!-- 1 6 --> 
<td align="left" bgcolor="#FFFFFF" > 
&nbsp; 
</td> 
<!-- 1 7 --> 
<td align="left" bgcolor="#FFFFFF" > 
Shne 
</td> 
<!-- 1 8 --> 
<td align="left" bgcolor="#FFFFFF" > 
&nbsp; 
</td> 
<!-- 1 9 --> 
<td align="left" bgcolor="#FFFFFF" > 
&nbsp; 
</td> 
<!-- 1 13 --> 
<td align="left" bgcolor="#FFFFFF" > 
&nbsp; 
</td> 
</tr> 

我已經測試了一個和其他一些人,但我並沒有到達他們爲我工作: Using JSoup To Extract HTML Table Contents

+0

你能解釋'table-Array'有點更詳細嗎? – ollo

回答

2

下面是一些示例代碼,你如何選擇只有標題:

Element tableHeader = doc.select("tr").first(); 


for(Element element : tableHeader.children()) 
{ 
    // Here you can do something with each element 
    System.out.println(element.text()); 
} 

你得到Document由...

  1. 解析文件Document doc = Jsoup.parse(f, null);(其中f是鐵道部的相關信息的Filenull的字符集,請參閱jsoup文檔)

  2. 解析網站Document doc = Jsoup.connect("http://your.url.here").get();(千萬不要錯過http://

輸出:

Kl. 
Std. 
Lehrer 
Fach 
Raum 
VLehrer 
VFach 
VRaum 
Info 

現在,如果你需要一個數組(或更好的List)的所有條目,你可以創建其中每一條目的所有信息存儲在一個新的類。接下來,通過jsoup解析Html,並填充該類的所有字段以及將其添加到列表中。

// Note: all values are strings - you'll need to use better types (int, enum whatever) here. But for an example its enough. 
public class Entry 
{ 
    private String klasse; 
    private String stunde; 
    private String lehrer; 
    private String fach; 
    private String raum; 
    private String vLehrer; 
    private String vFach; 
    private String vRaum; 
    private String info; 


    // constructor(s) and getter/setter 

    /* 
    * Btw. it's a good idea using two constructors here: one with all arguments and one empty. So you can create a new instance without knowing any data and add it with setter-methods afterwards. 
    */ 
} 

接下來的代碼至極填充您的輸入(包括它們的存儲列表。):

List<Entry> entries = new ArrayList<>();  // All entries are saved here 
boolean firstSkipped = false;     // Used to skip first 'tr' tag 


for(Element element : doc.select("tr"))  // Select all 'tr' tags from document 
{ 
    // Skip the first 'tr' tag since it's the header 
    if(!firstSkipped) 
    { 
     firstSkipped = true; 
     continue; 
    } 

    int index = 0;        // Instead of index you can use 0, 1, 2, ... 
    Entry tableEntry = new Entry(); 
    Elements td = element.select("td");   // Select all 'td' tags of the 'tr' 

    // Fill your entry 
    tableEntry.setKlasse(td.get(index++).text()); 
    tableEntry.setStunde(td.get(index++).text()); 
    tableEntry.setLehrer(td.get(index++).text()); 
    tableEntry.setFach(td.get(index++).text()); 
    tableEntry.setRaum(td.get(index++).text()); 
    tableEntry.setvLehrer(td.get(index++).text()); 
    tableEntry.setvFach(td.get(index++).text()); 
    tableEntry.setInfo(td.get(index++).text()); 

    entries.add(tableEntry);     // Finally add it to the list 
} 

如果您使用的HTML代碼的第一篇文章,你會得到這樣的輸出:

[Entry{klasse= , stunde=4, lehrer=Méta, fach=HU, raum= , vLehrer=Shne, vFach= , vRaum=null, info= }] 

注:我簡單地使用System.out.println(entries);。所以輸出的格式是從toString()方法Entry


請參閱Jsoup documentation,尤其是一個jsoup selector api

+0

非常感謝您的工作:)我會在接下來的幾天內測試它。 – Niklas

+0

它的工作原理,我想要它! – Niklas