聊一聊爬蟲那點事兒(二)

本文是學習java爬蟲的第二篇,第一篇在爬蟲入門第一篇

jsoup 是一款Java 的HTML解析器,可直接解析某個URL地址、HTML文本內容。它提供了一套非常省力的API,可通過DOM,CSS以及類似於jQuery的操作方法來取出和操作數據。
在這裏插入圖片描述

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解析

3.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解析工具使用

3.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.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());

3.4 使用dom方式遍歷文檔

元素獲取:

  1. 根據id查詢元素getElementById
  2. 根據標籤獲取元素getElementsByTag3
  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();

3.5 使用選擇器語法查找元素

jsoup elements對象支持類似於CSS (或jquery)的選擇器語法,來實現非常強大和靈活的查找功能。這個select方法在DocumentElement,或Elements對象中都可以使用。且是上下文相關的,因此可實現指定元素的過濾,或者鏈式選擇訪問。Select方法將返回一個Elements集合,並提供一組方法來抽取和處理結果。

Selector選擇器概述
elector選擇器概述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();

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();

爬蟲案例

基於springboot的java爬蟲
可以去這篇文章看看!
長路漫漫,JAVA爲伴!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章