我想分别解析每个<tr>
块的字符串,我的意思是变量;
每个字符串值都必须像这样;
“ 美国威斯康星航空1965 10.7最高1980 0 0根据与美国航空公司的合同 ”
我的数据保留了数据字符串变量
此HTML:
<tr>
<td bgcolor="#BAACA3">Air Wisconsin</td>
<td bgcolor="#BAACA3">USA</td>
<td align="center" bgcolor="#BAACA3">1965</td>
<td align="center" bgcolor="#BAACA3">10.7</td>
<td align="center" bgcolor="#BAACA3">high</td>
<td align="center" bgcolor="#BAACA3">1980</td>
<td align="center" bgcolor="#BAACA3">0</td>
<td align="center" bgcolor="#BAACA3">0</td>
<td bgcolor="#BAACA3">Under contract to US Airways</td>
</tr>
<tr>
<td bgcolor="#BAACA3">Aeroflot Russian Airlines</td>
<td bgcolor="#BAACA3">Russia</td>
<td align="center" bgcolor="#BAACA3">2000</td>
<td align="center" bgcolor="#BAACA3">5.8</td>
<td align="center" bgcolor="#BAACA3">high</td>
<td align="center" bgcolor="#BAACA3">1996</td>
<td align="center" bgcolor="#BAACA3">0</td>
<td align="center" bgcolor="#BAACA3">0</td>
<td bgcolor="#BAACA3"> </td>
</tr>
这里是HTML解析器;
try{
final StringBuilder builder = new StringBuilder();
Document doc = Jsoup.connect("http://www.planecrashinfo.com/accidents.htm").get();
title = doc.title();
Elements links = doc.select("td.BAACA3");
// builder.append(title).append("\n");
for (Element link : links) {
builder.append("\n").append("").append(link.text());
Datas= builder.toString();
}
}catch (Exception e){
e.printStackTrace();
}
答案 0 :(得分:0)
我创建了一个简单的函数来解析您的数据。请检查它是否符合您的要求。
import java.util.regex.Pattern
const val data = """
<tr>
<td bgcolor="#BAACA3">Air Wisconsin</td>
<td bgcolor="#BAACA3">USA</td>
<td align="center" bgcolor="#BAACA3">1965</td>
<td align="center" bgcolor="#BAACA3">10.7</td>
<td align="center" bgcolor="#BAACA3">high</td>
<td align="center" bgcolor="#BAACA3">1980</td>
<td align="center" bgcolor="#BAACA3">0</td>
<td align="center" bgcolor="#BAACA3">0</td>
<td bgcolor="#BAACA3">Under contract to US Airways</td>
</tr>
<tr>
<td bgcolor="#BAACA3">Aeroflot Russian Airlines</td>
<td bgcolor="#BAACA3">Russia</td>
<td align="center" bgcolor="#BAACA3">2000</td>
<td align="center" bgcolor="#BAACA3">5.8</td>
<td align="center" bgcolor="#BAACA3">high</td>
<td align="center" bgcolor="#BAACA3">0</td>
<td align="center" bgcolor="#BAACA3">0</td>
<td bgcolor="#BAACA3"> </td>
</tr>
"""
val regExp: Pattern = Pattern.compile(".+>(.+)</td>")
fun main() {
val lineList = parseData(data)
for (line in lineList) {
println(line)
}
}
private fun parseData(data: String): MutableList<String> {
val lineList = mutableListOf<String>()
val trs = data.split("</tr>")
for (tr in trs) {
var x = ""
val lines = tr.split("\n")
for (line in lines) {
val matcher = regExp.matcher(line)
if (matcher.matches()) {
val y = matcher.group(1)
if (y.trim() != " ") {
x += " $y"
}
}
}
lineList.add(x)
}
return lineList
}
输出
Air Wisconsin USA 1965 10.7 high 1980 0 0 Under contract to US Airways
Aeroflot Russian Airlines Russia 2000 5.8 high 0 0
尽管它在Koltin中,但转换为Java
很容易。
如果您需要任何帮助,请告诉我。
答案 1 :(得分:0)
我已经解决了我的问题,解决方法在这里;
int i =0;
try{
final StringBuilder builder = new StringBuilder();
String Blank="";
String Explanation="";
String dizi[] = {"AIRLINE : ","COUNTRY : ","BEGAN OPERATION : ","AVERAGE AGE OF FLEET : ","PASSENGER VOLUME :","LAST FATAL ACCIDENT : ",
"FATAL ACCIDENTS LAST 10 YRS : ","FATAL ACCIDENTS LAST 20 YRS : ","EXPLANATION : "};
Document doc = Jsoup.connect("http://www.planecrashinfo.com/accidents.htm").get();
title = doc.title();
Elements links = doc.select("td");
// builder.append(title).append("\n");
for (Element link : links) {
if (i==dizi.length){
i=0;
Blank=":DENEME:";
}else{
Blank="";
}
builder.append("\n").append(Blank+dizi[i]).append(link.text()+Explanation);
Datas= builder.toString();
i++;
}
}catch (Exception e){
e.printStackTrace();
}
和字符串生成器;
String currentString = Datas;
String[] separated = currentString.split(":DENEME:");
for (int i=1; i<separated.length;i++) {
Person person = new Person(""+(i) + "", separated[i],"");
persons.add(person);
}