Jsoup: 用來進行 Html 的 Dom 元素內容解析
0. 項目準備
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
1. url 解析 👇
@Test
public void testUrl() throws Exception{
// 解析 URL 第一個參數是 URL, 第二個參數是超時時間(單位是毫秒)
Document doc = Jsoup.parse(new URL("http://www.itcast.cn"), 1000);
// 使用標籤選擇器,獲得網頁中 title 標籤內容
String title = doc.getElementsByTag("title").first().text();
System.out.println(title);
}
2. file 解析 👇
@Test
public void testFile() throws Exception{
Document doc = Jsoup.parse(new File("G:/IDEA/workspace/rc/src/main/resources/templates/rec-login.html"), "utf8");
String title = doc.getElementsByTag("title").first().text();
System.out.println(title);
}
3. dom 解析 👇
@Test
public void testDom() throws Exception{
Document doc = Jsoup.parse(new File("G:/IDEA/workspace/rc/src/main/resources/templates/rec-login.html"), "utf8");
// 根據 id 獲取元素
// Element element = doc.getElementById("registerForm");
// 根據標籤獲取元素
// Element element = doc.getElementsByTag("h2").first();
// 根據 class 獲取元素
// Element element = doc.getElementsByClass("formPanel").first();
// 根據屬性獲取元素
// Element element = doc.getElementsByAttribute("method").first();
// 根據屬性及屬性值獲取元素
Element element = doc.getElementsByAttributeValue("method", "post").first();
System.out.println(element.text());
}
4. data 解析 👇
@Test
public void testData() throws Exception{
Document doc = Jsoup.parse(new File("G:/IDEA/workspace/rc/src/main/resources/templates/rec-login.html"), "utf8");
Element element = doc.getElementById("registerForm");
String str = "";
// 從元素中獲取 id
// str = element.id();
// 從元素中獲取 className
// str = element.className();
// 從元素中獲取 attr
// str = element.attr("id");
// 從元素中獲取所有 attr
// str = element.attributes().toString();
// 從元素中獲取文本
str = element.text();
System.out.println(str);
}
5. selector 解析 👇
@Test
public void testSelector() throws Exception{
Document doc = Jsoup.parse(new File("G:/IDEA/workspace/rc/src/main/resources/templates/rec-login.html"), "utf8");
// el#id 查找帶有該id的元素
// Element element = doc.select("form#loginContent").first();
// el.class 查找帶有該class的元素
// Element element = doc.select("div.form.login-form").first();
// el[attr] 查找帶有該屬性的元素
// Element element = doc.select("div[id]").first();
// 任意組合 el[attr=val].class 查找帶有該屬性=屬性值,該class的元素
// Element element = doc.select("div[id=phizzForm].form.login-form").first();
// ancestor child 查找父元素下的子元素
// Element element = doc.select("#loginContent h2").first();
// parent > child 查找父元素下的直接子元素
// Element element = doc.select(".formPanel > p").first();
// parent > * 查找父元素下的所有子元素
for(Element e : doc.select("fieldset > *")){
System.out.println(e.text());
}
}