# Java jsoup怎么使用
## 一、jsoup簡介
jsoup是一個用于處理實際HTML的Java庫。它提供了一套非常便捷的API,可以通過DOM、CSS以及類似jQuery的操作方法來提取和操作數據。jsoup的主要功能包括:
1. 從URL、文件或字符串中解析HTML
2. 使用DOM遍歷或CSS選擇器查找和提取數據
3. 操作HTML元素、屬性和文本
4. 清除用戶提交的內容以防止XSS攻擊
5. 輸出整潔的HTML
jsoup非常適合用于:
- 網頁抓取和數據提取
- 解析和清理HTML
- 網頁內容分析和處理
## 二、環境準備
### 1. 添加jsoup依賴
Maven項目在pom.xml中添加:
```xml
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.16.1</version> <!-- 使用最新版本 -->
</dependency>
Gradle項目:
implementation 'org.jsoup:jsoup:1.16.1'
可以從jsoup官網下載jar文件,然后手動添加到項目中。
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
Document doc = Jsoup.connect("https://example.com/").get();
String title = doc.title();
File input = new File("/path/to/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "https://example.com/");
Document doc = Jsoup.connect("https://example.com").get();
// 獲取標題
String title = doc.title();
// 獲取特定id的元素
Element content = doc.getElementById("content");
// 獲取所有鏈接
Elements links = doc.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}
// 選擇帶有href屬性的a元素
Elements links = doc.select("a[href]");
// 選擇class為masthead的div
Elements masthead = doc.select("div.masthead");
// 選擇直接子元素
Elements resultLinks = doc.select("h3.r > a");
Document doc = Jsoup.parse("<div><p>Lorem ipsum.</p></div>");
// 修改屬性
Element div = doc.select("div").first();
div.attr("class", "newClass");
// 添加類
div.addClass("anotherClass");
// 修改文本內容
div.text("New text content");
// 修改HTML內容
div.html("<p>New <b>HTML</b> content</p>");
// 追加內容
div.append("<p>Appended paragraph</p>");
// 在元素前插入內容
div.prepend("<p>Prepended paragraph</p>");
// 獲取登錄表單
Document doc = Jsoup.connect("http://example.com/login").get();
Element loginForm = doc.selectFirst("form#login");
// 準備表單數據
Connection.Response res = Jsoup.connect("http://example.com/login")
.data("username", "myUser")
.data("password", "myPass")
.method(Connection.Method.POST)
.execute();
// 獲取登錄后的會話cookie
Map<String, String> cookies = res.cookies();
// 使用cookie訪問受保護頁面
Document protectedPage = Jsoup.connect("http://example.com/protected")
.cookies(cookies)
.get();
Document doc = Jsoup.connect("https://example.com/news").get();
// 獲取絕對URL
Elements links = doc.select("a[href]");
for (Element link : links) {
String absUrl = link.attr("abs:href"); // 轉換為絕對URL
System.out.println(absUrl);
}
String unsafeHtml = "<p><a href='http://example.com/' onclick='stealCookies()'>Link</a></p>";
// 使用白名單清理
String safeHtml = Jsoup.clean(unsafeHtml,
Whitelist.basic()
.addTags("p")
.addAttributes("a", "href"));
System.out.println(safeHtml);
// 輸出: <p><a href="http://example.com/">Link</a></p>
Document doc = Jsoup.connect("https://example.com")
.proxy("proxy.example.com", 8080) // 設置代理
.userAgent("Mozilla/5.0") // 設置User-Agent
.timeout(10000) // 設置超時時間
.get();
public class NewsCrawler {
public static void main(String[] args) throws IOException {
String url = "https://news.example.com";
Document doc = Jsoup.connect(url).get();
Elements newsHeadlines = doc.select(".news-item h3 a");
for (Element headline : newsHeadlines) {
String title = headline.text();
String link = headline.attr("abs:href");
System.out.println("標題: " + title);
System.out.println("鏈接: " + link);
System.out.println("------------------");
}
}
}
public class TableExtractor {
public static void main(String[] args) throws IOException {
String url = "https://example.com/data-table";
Document doc = Jsoup.connect(url).get();
Element table = doc.select("table.data").first();
Elements rows = table.select("tr");
for (Element row : rows) {
Elements cols = row.select("td");
for (Element col : cols) {
System.out.print(col.text() + "\t");
}
System.out.println();
}
}
}
public class HtmlBuilder {
public static void main(String[] args) {
Document doc = Document.createShell("");
doc.title("Generated Page");
Element body = doc.body();
body.appendElement("h1").text("Welcome to my page");
Element div = body.appendElement("div")
.attr("class", "content");
div.appendElement("p")
.text("This is a paragraph.")
.addClass("highlight");
System.out.println(doc);
}
}
// 推薦 Element content = doc.selectFirst(“div.content”); content.select(“p.small”);
3. **合理設置超時**:根據網絡情況調整連接超時時間
4. **使用連接池**:對于大量請求,考慮使用連接池
5. **并行處理**:對于獨立的任務可以使用多線程
## 七、常見問題解決
### 1. 處理SSL證書問題
```java
// 跳過SSL驗證(不推薦生產環境使用)
Connection connection = Jsoup.connect("https://example.com");
connection.sslSocketFactory(SSLSocketClient.getSSLSocketFactory());
Document doc = connection.get();
Document doc = Jsoup.connect("https://example.com")
.followRedirects(true) // 啟用重定向
.get();
Document doc = Jsoup.connect("https://example.com")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
.referrer("http://www.google.com")
.header("Accept-Language", "en-US")
.get();
// 使用流式處理大文件
FileInputStream fis = new FileInputStream(new File("large.html"));
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
Document doc = Jsoup.parse(sb.toString());
特性 | jsoup | HtmlUnit | Selenium |
---|---|---|---|
執行JavaScript | 不支持 | 支持 | 支持 |
輕量級 | 是 | 中等 | 重 |
學習曲線 | 低 | 中等 | 中等 |
適用場景 | 簡單HTML解析 | 復雜網頁交互 | 瀏覽器自動化測試 |
性能 | 高 | 中等 | 低 |
jsoup是一個功能強大且易于使用的HTML解析庫,特別適合Java開發者進行網頁內容提取和操作。通過本文的介紹,你應該已經掌握了:
在實際項目中,建議根據具體需求選擇合適的工具。對于簡單的HTML解析和內容提取,jsoup無疑是最佳選擇之一;對于需要執行JavaScript的復雜頁面,可能需要考慮HtmlUnit或Selenium等工具。
希望本文能幫助你快速掌握jsoup的使用,在實際開發中提高工作效率! “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。