引入依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
try {
URL url = new URL("html地址url");
URLConnection connection =url.openConnection();
connection.setRequestProperty("User-Agent","Mozilla/5.0");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(inputStream));
String line;
StringBuilder htmlContent = new StringBuilder();
System.out.println("----------"+url);
while((line =reader.readLine())!= null) {
htmlContent.append(line);
}
Document doc = Jsoup.parse(htmlContent.toString());
// 假设要获取id为aaa的div元素下数据,根据 id 定位指定的 div 元素
Element unitInfoDiv = doc.getElementById("aaa");
// 如果找到了对应 id 的 div 元素
if (unitInfoDiv != null) {
// 获取 div 元素下的所有文本内容(包括子元素)
String divContent = unitInfoDiv.text();
// 切分数据
String[] parts = divContent.split("\\s+");
// 提取所需信息
String label1 = "";
String label2 = "";
String label3 = "";
for (int i = 0; i < parts.length - 1; i++) {
if (parts[i].equals("label1")) {
label1 = parts[i + 1];
} else if (parts[i].equals("label2")) {
label2 = parts[i + 1];
} else if (parts[i].equals("label3")) {
label3 = parts[i + 1];
}
}
// 输出提取的信息
System.out.println("label1: " + label1);
System.out.println("label2: " + label2);
System.out.println("label3: " + label3);
}
reader.close();
}catch(Exception e){
e.printStackTrace();
}