一、Jsoup介紹
我們抓取到頁面之后,還需要對頁面進行解析。可以使用字符串處理工具解析頁面,也可以使用正則表達式,但是這些方法都會帶來很大的開發成本,所以我們需要使用一款專門解析html頁面的技術。
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.
jsoup 是一款Java 的HTML解析器,可直接解析某個URL地址、HTML文本內容。它提供了一套非常省力的API,可通過DOM,CSS以及類似於jQuery的操作方法來取出和操作數據。
這個給出jsoup的中文幫助文檔https://www.open-open.com/jsoup/
jsoup的主要功能如下:
1.從一個URL,文件或字符串中解析HTML;
2.使用DOM或CSS選擇器來查找、取出數據;
3.可操作HTML元素、屬性、文本;
先加入Jsoup依賴:
!--Jsoup--> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.10.3</version> </dependency> <!--測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--工具--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
二、Jsoup解析
1、解析URL
Jsoup可以直接輸入url,它會發起請求並獲取數據,封裝為Document對象
@Test public void testJsoupUrl() throws Exception { // 解析url地址 Document document = Jsoup.parse(new URL("http://www.itcast.cn/"), 1000); //獲取title的內容 Element title = document.getElementsByTag("title").first(); System.out.println(title.text()); }
PS:雖然使用Jsoup可以替代HttpClient直接發起請求解析數據,但是往往不會這樣用,因為實際的開發過程中,需要使用到多線程,連接池,代理等等方式,而jsoup對這些的支持並不是很好,所以我們一般把jsoup僅僅作為Html解析工具使用
2、解析字符串
先准備以下html文件
<html> <head> <title>傳智播客官網-一樣的教育,不一樣的品質</title> </head> <body> <div class="city"> <h3 id="city_bj">北京中心</h3> <fb:img src="/2018czgw/images/slogan.jpg" class="slogan"/> <div class="city_in"> <div class="city_con" style="display: none;"> <ul> <li id="test" class="class_a class_b"> <a href="http://www.itcast.cn" target="_blank"> <span class="s_name">北京</span> </a> </li> <li> <a href="http://sh.itcast.cn" target="_blank"> <span class="s_name">上海</span> </a> </li> <li> <a href="http://gz.itcast.cn" target="_blank"> <span abc="123" class="s_name">廣州</span> </a> </li> <ul> <li>天津</li> </ul> </ul> </div> </div> </div> </body> </html>
Jsoup可以直接輸入字符串,並封裝為Document對象
@Test public void testJsoupString() throws Exception { //讀取文件獲取 String html = FileUtils.readFileToString(new File("D:\\jsoup.html"), "UTF-8"); // 解析字符串 Document document = Jsoup.parse(html); //獲取title的內容 Element title = document.getElementsByTag("title").first(); System.out.println(title.text()); }
3、解析文件
Jsoup可以直接解析文件,並封裝為Document對象
@Test public void testJsoupHtml() throws Exception { // 解析文件 Document document = Jsoup.parse(new File("D:\\jsoup.html"),"UTF-8"); //獲取title的內容 Element title = document.getElementsByTag("title").first(); System.out.println(title.text()); }
4、使用dom方式遍歷文檔
關於dom的說明
HTML DOM 定義了訪問和操作 HTML 文檔的標准方法。
DOM 將 HTML 文檔表達為樹結構。
就像是下圖這樣的HTML dom樹結構
w3school關於dom的教程:https://www.w3school.com.cn/htmldom/index.asp
元素獲取
1.根據id查詢元素getElementById
2.根據標簽獲取元素getElementsByTag
3.根據class獲取元素getElementsByClass
4.根據屬性獲取元素getElementsByAttribute
//1. 根據id查詢元素getElementById Element element = document.getElementById("city_bj"); //2. 根據標簽獲取元素getElementsByTag element = document.getElementsByTag("title").first(); //3. 根據class獲取元素getElementsByClass element = document.getElementsByClass("s_name").last(); //4. 根據屬性獲取元素getElementsByAttribute element = document.getElementsByAttribute("abc").first(); element = document.getElementsByAttributeValue("class", "city_con").first();
元素中獲取數據
1.從元素中獲取id
2.從元素中獲取className
3.從元素中獲取屬性的值attr
4.從元素中獲取所有屬性attributes
5.從元素中獲取文本內容text
//獲取元素 Element element = document.getElementById("test"); //1. 從元素中獲取id String str = element.id(); //2. 從元素中獲取className str = element.className(); //3. 從元素中獲取屬性的值attr str = element.attr("id"); //4. 從元素中獲取所有屬性attributes str = element.attributes().toString(); //5. 從元素中獲取文本內容text str = element.text();
5、Selector選擇器
jsoup elements對象支持類似於CSS (或jquery)的選擇器語法,來實現非常強大和靈活的查找功能。這個select 方法在Document, Element,或Elements對象中都可以使用。且是上下文相關的,因此可實現指定元素的過濾,或者鏈式選擇訪問。
Select方法將返回一個Elements集合,並提供一組方法來抽取和處理結果。
tagname: 通過標簽查找元素,比如:span
#id: 通過ID查找元素,比如:# city_bj
.class: 通過class名稱查找元素,比如:.class_a
[attribute]: 利用屬性查找元素,比如:[abc]
[attr=value]: 利用屬性值來查找元素,比如:[class=s_name]
//tagname: 通過標簽查找元素,比如:span Elements span = document.select("span"); for (Element element : span) { System.out.println(element.text()); } //#id: 通過ID查找元素,比如:#city_bjj String str = document.select("#city_bj").text(); //.class: 通過class名稱查找元素,比如:.class_a str = document.select(".class_a").text(); //[attribute]: 利用屬性查找元素,比如:[abc] str = document.select("[abc]").text(); //[attr=value]: 利用屬性值來查找元素,比如:[class=s_name] str = document.select("[class=s_name]").text();
6、Selector選擇器的組合使用
el#id: 元素+ID,比如: h3#city_bj
el.class: 元素+class,比如: li.class_a
el[attr]: 元素+屬性名,比如: span[abc]
任意組合: 比如:span[abc].s_name
ancestor child: 查找某個元素下子元素,比如:.city_con li 查找"city_con"下的所有li
parent > child: 查找某個父元素下的直接子元素,比如:
.city_con > ul > li 查找city_con第一級(直接子元素)的ul,再找所有ul下的第一級li
parent > *: 查找某個父元素下所有直接子元素
//el#id: 元素+ID,比如: h3#city_bj String str = document.select("h3#city_bj").text(); //el.class: 元素+class,比如: li.class_a str = document.select("li.class_a").text(); //el[attr]: 元素+屬性名,比如: span[abc] str = document.select("span[abc]").text(); //任意組合,比如:span[abc].s_name str = document.select("span[abc].s_name").text(); //ancestor child: 查找某個元素下子元素,比如:.city_con li 查找"city_con"下的所有li str = document.select(".city_con li").text(); //parent > child: 查找某個父元素下的直接子元素, //比如:.city_con > ul > li 查找city_con第一級(直接子元素)的ul,再找所有ul下的第一級li str = document.select(".city_con > ul > li").text(); //parent > * 查找某個父元素下所有直接子元素.city_con > * str = document.select(".city_con > *").text();