gpt4 book ai didi

java - 如何使用jsoup从html文件中的强标签中检索数据?

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:48 25 4
gpt4 key购买 nike

我有一些 html 数据,例如

<div class="bs-example">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">ABC</h3>
</div>
<div class="panel-body">
<div class="slimScroller" style="height:280px; position: relative;" data-rail-visible="1" data-always-visible="1">
<strong>Name:</strong>
<a href="https://ABC"> </a><br />
<strong>ID No:</strong> XXXXX<br />
<strong>Status:</strong> ACTIVE<br />
<strong>Class:</strong> 5<br />
<strong>Category:</strong> A<br />
<strong>Marks:</strong> 500<br />
</div>
</div>
</div>
</div>

我想要输出为(多个学生数据):

 Name: ABC
ID No.: XXXXX
Status: Active
Class: 5
Category: A
Marks: 500

如何使用 jsoup 或任何其他方式获取此数据?请帮忙。

最佳答案

您可以使用 Element.nextElementSibling() 或/和 Element.nextSibling() 来获取所需的输出。

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Exam {
public static void main(String[] args) {
String html = "<div class=\"bs-example\">" +
" <div class=\"panel panel-primary\">" +
" <div class=\"panel-heading\">" +
" <h3 class=\"panel-title\">ABC</h3>" +
" </div>" +
" <div class=\"panel-body\">" +
" <div class=\"slimScroller\" style=\"height:280px; position: relative;\" data-rail-visible=\"1\" data-always-visible=\"1\">" +
" <strong>Name:</strong>" +
" <a href=\"https://ABC\"> </a><br />" +
" <strong>ID No:</strong> XXXXX<br />" +
" <strong>Status:</strong> ACTIVE<br />" +
" <strong>Class:</strong> 5<br />" +
" <strong>Category:</strong> A<br />" +
" <strong>Marks:</strong> 500<br />" +
" </div>" +
" </div>" +
" </div>" +
"</div>";

Document doc = Jsoup.parse(html);
Elements eles = doc.select("div.slimScroller strong");
for(Element e :eles)
System.out.println(e.text() +
( e.nextElementSibling().tagName().equals("a")?
e.nextElementSibling().attr("href").replace("https://", ""):
e.nextSibling().toString()));
}
}

关于java - 如何使用jsoup从html文件中的强标签中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46404771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com