Java 使用JSoup从表中提取数据
我想用JSoup框架提取这个表,将内容保存在一个“表”数组中。第一个tr标记是表头。以下所有内容(不包括)描述了内容Java 使用JSoup从表中提取数据,java,html,arrays,jsoup,html-table,Java,Html,Arrays,Jsoup,Html Table,我想用JSoup框架提取这个表,将内容保存在一个“表”数组中。第一个tr标记是表头。以下所有内容(不包括)描述了内容 <table style=h2 width=100% cellspacing="0" cellpadding="4" border="1" bgColor="#FFFFFF"> <tr> <td align="left" bgcolor="#9999FF" > <!-- 0 --> Kl. </td> <td al
<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" >
</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" >
</td>
<!-- 1 7 -->
<td align="left" bgcolor="#FFFFFF" >
Shne
</td>
<!-- 1 8 -->
<td align="left" bgcolor="#FFFFFF" >
</td>
<!-- 1 9 -->
<td align="left" bgcolor="#FFFFFF" >
</td>
<!-- 1 13 -->
<td align="left" bgcolor="#FFFFFF" >
</td>
</tr>
吉隆坡。
性病。
莱勒
法赫
劳姆
元首
VFach
弗拉姆
信息
4.
梅塔
胡
施恩
我已经测试了这一个和其他一些,但我没有到达他们为我工作:
以下是一些示例代码,说明如何仅选择标题:
Element tableHeader = doc.select("tr").first();
for( Element element : tableHeader.children() )
{
// Here you can do something with each element
System.out.println(element.text());
}
您可以通过以下方式获得文档
:
文档doc=Jsoup.parse(f,null)
(其中f
是文件
和null
字符集,有关mor信息,请参阅jsoup文档)文档doc=Jsoup.connect(“http://your.url.here).get()代码>(不要错过http://
)
Kl.
Std.
Lehrer
Fach
Raum
VLehrer
VFach
VRaum
Info
现在,如果您需要所有条目的数组(或者更好的
列表
),您可以创建一个新类,其中存储每个条目的所有信息。接下来,通过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.
*/
}
接下来,填充您的条目的代码(包括存储它们的列表):
注意:我只是使用了System.out.println(条目)代码>。因此,输出的格式来自Entry
的toString()
方法
请查看,尤其是针对的。您能更详细地解释一下“表数组”吗?非常感谢您的工作:)我将在未来几天内对其进行测试。
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
}
[Entry{klasse= , stunde=4, lehrer=Méta, fach=HU, raum= , vLehrer=Shne, vFach= , vRaum=null, info= }]