1、jsoup介紹
jsoup是一款java的HTML解析器,可直接解析某個URL位址、HTML檔案内容。它提供了一套非常省力的API,可通過DOM、CSS以及類似Jquery的操作方法取出和操作資料。
jsoup的主要功能如下:
- 從一個URL,檔案或者字元串中解析HTML。
- 使用DOM或者CSS選擇器來直接查找、取出資料。
- 可操作HTML元素、屬性、文本。
2、jsoup和HttpClient的姻緣
雖然使用jsoup可以替代HttpClient直接發起請求解析資料,但是往往不這樣用,因為實際開發過程中,需要使用多線程、連接配接池、代理等等方式,而jsoup對這些的支援不是很好,是以我們一般隻把jsoup僅僅作為HTML解析工具使用,可以用HttpClient擷取HTML,然後交給jsoup解析。
3、建立一個類并配置pom.xml
如何建立項目請看:https://blog.csdn.net/weixin_44588495/article/details/90580722
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPR5UNRR0T0UFROBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL5ETOxETMzADM3ITNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.crawler</groupId>
<artifactId>Crawler-first-one</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<!-- <scope>test</scope>-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</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.4</version>
</dependency>
</dependencies>
</project>
利用junit來測試
4、解析URL
@Test
//解析Url
public void testUrl()throws Exception{
//解析url位址,第一個參數是通路的url,第二個參數是通路時候的逾時時間
Document doc = Jsoup.parse(new URL("http://itcast.cn"),1000);
//使用标簽選擇器,擷取title标簽中的内容
String title = doc.getElementsByTag("title").first().text();
System.out.println(title);
}
運作結果:
5、解析字元串
@Test
//解析字元串
public void testString()throws Exception{
//假設用httpClient讀取到了html頁面
String content = "<title>哈哈哈</title>";
Document doc = Jsoup.parse(content);
String title = doc.getElementsByTag("title").first().text();
System.out.println(title);
}
運作結果:
6、解析檔案
@Test
//解析檔案
public void testFile()throws Exception{
Document doc = Jsoup.parse(new File("C:\\Users\\Administrator\\Desktop\\index.html"),"utf-8");
String title = doc.getElementsByTag("title").first().text();
System.out.println(title);
}
運作結果:
7、完整代碼
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;
import java.io.File;
import java.net.URL;
public class JsoupFirstTest {
@Test
//解析Url
public void testUrl()throws Exception{
//解析url位址,第一個參數是通路的url,第二個參數是通路時候的逾時時間
Document doc = Jsoup.parse(new URL("http://itcast.cn"),1000);
//使用标簽選擇器,擷取title标簽中的内容
String title = doc.getElementsByTag("title").first().text();
System.out.println(title);
}
@Test
//解析字元串
public void testString()throws Exception{
//假設用httpClient讀取到了html頁面
String content = "<title>document</title>";
Document doc = Jsoup.parse(content);
String title = doc.getElementsByTag("title").first().text();
System.out.println(title);
}
@Test
//解析檔案
public void testFile()throws Exception{
Document doc = Jsoup.parse(new File("C:\\Users\\Administrator\\Desktop\\index.html"),"utf-8");
String title = doc.getElementsByTag("title").first().text();
System.out.println(title);
}
}