轉(zhuǎn)載至:
https://www.cnblogs.com/zhangyinhua/p/8037599.html
一邑遏、Jsoup概述
一、Jsoup概述
1.1片迅、簡介
jsoup 是一款Java 的HTML解析器弛房,可直接解析某個URL地址害驹、HTML文本內(nèi)容因苹。它提供了一套非常省力的API苟耻,
可通過DOM,CSS以及類似于jQuery的操作方法來取出和操作數(shù)據(jù)扶檐。
1.2凶杖、Jsoup的主要功能
1)從一個URL,文件或字符串中解析HTML
2)使用DOM或CSS選擇器來查找款筑、取出數(shù)據(jù)
3)可操作HTML元素智蝠、屬性、文本
注意:jsoup是基于MIT協(xié)議發(fā)布的奈梳,可放心使用于商業(yè)項目杈湾。
1.3、jsoup 的主要類層次結(jié)構(gòu)
二攘须、入門
2.1漆撞、解析和遍歷一個HTML文檔
如何解析一個HTML文檔:
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
其解析器能夠盡最大可能從你提供的HTML文檔來創(chuàng)見一個干凈的解析結(jié)果,無論HTML的格式是否完整叫挟。比如它可以處理:
1)沒有關(guān)閉的標(biāo)簽
<p>Lorem <p>Ipsum parses to <p>Lorem</p> <p>Ipsum</p>
2)隱式標(biāo)簽
它可以自動將 <td>Table data</td>包裝成<table><tr><td>?
3)創(chuàng)建可靠的文檔結(jié)構(gòu)
html標(biāo)簽包含head 和 body,在head只出現(xiàn)恰當(dāng)?shù)脑?
2.2署驻、一個文檔的對象模型
1)文檔由多個Elements和TextNodes組成
2)其繼承結(jié)構(gòu)如下:Document繼承Element繼承Node. TextNode繼承 Node.
3)一個Element包含一個子節(jié)點集合旺上,并擁有一個父Element窃这。他們還提供了一個唯一的子元素過濾列表。
三兆解、輸入
3.1埠巨、解析一個HTML字符串
1)存在問題
來自用戶輸入,一個文件或一個網(wǎng)站的HTML字符串勋桶,你可能需要對它進(jìn)行解析并取其內(nèi)容岂丘,或校驗其格式是否完整铜邮,
或想修改它。怎么辦秸苗? jsoup能夠幫你輕松解決這些問題
2)解決方法
使用靜態(tài)Jsoup.parse(String html) 方法或 Jsoup.parse(String html, String baseUri)
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
3)描述
A:
parse(String html, String baseUri) 這方法能夠?qū)⑤斎氲腍TML解析為一個新的文檔 (Document),參數(shù) baseUri 是用來將相對 URL 轉(zhuǎn)成絕對URL,
并指定從哪個網(wǎng)站獲取文檔弧可。如這個方法不適用,你可以使用 parse(String html) 方法來解析成HTML字符串如上面的示例。
B:
只要解析的不是空字符串搔确,就能返回一個結(jié)構(gòu)合理的文檔,其中包含(至少) 一個head和一個body元素华匾。
C:
一旦擁有了一個Document有鹿,你就可以使用Document中適當(dāng)?shù)姆椒ɑ蛩割?Element和Node中的方法來取得相關(guān)數(shù)據(jù)。
3.2稍味、解析一個body片斷
1)存在問題
假如你有一個HTML片斷 (比如. 一個 div 包含一對 p 標(biāo)簽; 一個不完整的HTML文檔) 想對它進(jìn)行解析油宜。這個HTML片斷可以是用戶提交的一條評論
或在一個CMS頁面中編輯body部分。
2)辦法
使用Jsoup.parseBodyFragment(String html)方法。
String html = "<div><p>Lorem ipsum.</p>";
Document doc = Jsoup.parseBodyFragment(html);
Element body = doc.body();
3)說明
A:
parseBodyFragment 方法創(chuàng)建一個空殼的文檔违寿,并插入解析過的HTML到body元素中湃交。假如你使用正常的 Jsoup.parse(String html) 方法搞莺,
通常你也可以得到相同的結(jié)果掂咒,但是明確將用戶輸入作為 body片段處理,以確保用戶所提供的任何糟糕的HTML都將被解析成body元素。
B:
Document.body() 方法能夠取得文檔body元素的所有子元素非剃,與 doc.getElementsByTag("body")相同诽偷。
3.3坤学、從一個URL加載一個Document
1)存在問題
你需要從一個網(wǎng)站獲取和解析一個HTML文檔压怠,并查找其中的相關(guān)數(shù)據(jù)。
2)解決方法
使用 Jsoup.connect(String url)方法:
Document doc = Jsoup.connect("http://example.com/").get();
String title = doc.title();
3)說明
connect(String url) 方法創(chuàng)建一個新的 Connection, 和 get() 取得和解析一個HTML文件飞苇。如果從該URL獲取HTML時發(fā)生錯誤菌瘫,便會拋出 IOException,應(yīng)適當(dāng)處理布卡。
Connection 接口還提供一個方法鏈來解決特殊請求雨让,具體如下:
Document doc = Jsoup.connect("http://example.com";)
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(3000)
.post();
這個方法只支持Web URLs (http和https 協(xié)議); 假如你需要從一個文件加載,可以使用 parse(File in, String charsetName) 代替忿等。
3.4栖忠、從一個文件加載文檔
1)存在問題
在本機(jī)硬盤上有一個HTML文件,需要對它進(jìn)行解析從中抽取數(shù)據(jù)或進(jìn)行修改贸街。
2)方法
可以使用靜態(tài) Jsoup.parse(File in, String charsetName, String baseUri) 方法:
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/";);
3)說明
A:
parse(File in, String charsetName, String baseUri) 這個方法用來加載和解析一個HTML文件庵寞。如在加載文件的時候發(fā)生錯誤,將拋出IOException薛匪,應(yīng)作適當(dāng)處理捐川。
B:
baseUri 參數(shù)用于解決文件中URLs是相對路徑的問題。如果不需要可以傳入一個空的字符串逸尖。
C:
另外還有一個方法parse(File in, String charsetName) 古沥,它使用文件的路徑做為 baseUri瘸右。 這個方法適用于如果被解析文件位于網(wǎng)站的本地文件系統(tǒng),
且相關(guān)鏈接也指向該文件系統(tǒng)岩齿。
四尊浓、數(shù)據(jù)抽取
4.1、使用DOM方法來遍歷一個文檔
1)存在問題
你有一個HTML文檔要從中提取數(shù)據(jù)纯衍,并了解這個HTML文檔的結(jié)構(gòu)栋齿。
2)方法
將HTML解析成一個Document之后,就可以使用類似于DOM的方法進(jìn)行操作襟诸。
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a"); for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}
3)說明
Elements這個對象提供了一系列類似于DOM的方法來查找元素瓦堵,抽取并處理其中的數(shù)據(jù)。具體如下:
A:查看元素
getElementById(String id)
getElementsByTag(String tag)
getElementsByClass(String className)
getElementsByAttribute(String key) (and related methods)
Element siblings: siblingElements(), firstElementSibling(), lastElementSibling(); nextElementSibling(), previousElementSibling()
B:元素數(shù)據(jù)
attr(String key)獲取屬性attr(String key, String value)設(shè)置屬性
attributes()獲取所有屬性
id(), className() and classNames()
text()獲取文本內(nèi)容text(String value) 設(shè)置文本內(nèi)容
html()獲取元素內(nèi)HTMLhtml(String value)設(shè)置元素內(nèi)的HTML內(nèi)容
outerHtml()獲取元素外HTML內(nèi)容
data()獲取數(shù)據(jù)內(nèi)容(例如:script和style標(biāo)簽)
tag() and tagName()
C:操作HTML和文本
append(String html), prepend(String html)
appendText(String text), prependText(String text)
appendElement(String tagName), prependElement(String tagName)
html(String value)
4.2歌亲、使用選擇器語法來查找元素
1)存在問題
你想使用類似于CSS或jQuery的語法來查找和操作元素菇用。
2)方法
可以使用Element.select(String selector) 和 Elements.select(String selector) 方法實現(xiàn):
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
Elements links = doc.select("a[href]"); //帶有href屬性的a元素
Elements pngs = doc.select("img[src$=.png]"); //擴(kuò)展名為.png的圖片
Element masthead = doc.select("div.masthead").first(); //class等于masthead的div標(biāo)簽
Elements resultLinks = doc.select("h3.r > a"); //在h3元素之后的a元素</pre>
3)說明
jsoup elements對象支持類似于CSS (或jquery)的選擇器語法,來實現(xiàn)非常強(qiáng)大和靈活的查找功能陷揪。.
這個select 方法在Document, Element,或Elements對象中都可以使用惋鸥。且是上下文相關(guān)的,因此可實現(xiàn)指定元素的過濾悍缠,或者鏈?zhǔn)竭x擇訪問卦绣。
Select方法將返回一個Elements集合,并提供一組方法來抽取和處理結(jié)果飞蚓。
A:Selector選擇器概述
tagname: 通過標(biāo)簽查找元素滤港,比如:a
ns|tag: 通過標(biāo)簽在命名空間查找元素,比如:可以用 fb|name 語法來查找 <fb:name> 元素
#id: 通過ID查找元素趴拧,比如:#logo
.class: 通過class名稱查找元素溅漾,比如:.masthead
[attribute]: 利用屬性查找元素,比如:[href]
[^attr]: 利用屬性名前綴來查找元素著榴,比如:可以用[^data-] 來查找?guī)в蠬TML5 Dataset屬性的元素
[attr=value]: 利用屬性值來查找元素添履,比如:[width=500]
[attr^=value], [attr$=value], [attr=value]: 利用匹配屬性值開頭、結(jié)尾或包含屬性值來查找元素脑又,比如:[href=/path/]
[attr~=regex]: 利用屬性值匹配正則表達(dá)式來查找元素暮胧,比如: img[src~=(?i).(png|jpe?g)]
*: 這個符號將匹配所有元素
B:Selector選擇器組合使用
el#id: 元素+ID,比如: div#logo
el.class: 元素+class挂谍,比如: div.masthead
el[attr]: 元素+class叔壤,比如: a[href]
任意組合,比如:a[href].highlight
ancestor child: 查找某個元素下子元素口叙,比如:可以用.body p 查找在"body"元素下的所有 p元素
parent > child: 查找某個父元素下的直接子元素炼绘,比如:可以用div.content > p 查找 p 元素,也可以用body > * 查找body標(biāo)簽下所有直接子元素
siblingA + siblingB: 查找在A元素之前第一個同級元素B妄田,比如:div.head + div
siblingA ~ siblingX: 查找A元素之前的同級X元素俺亮,比如:h1 ~ p
el, el, el:多個選擇器組合驮捍,查找匹配任一選擇器的唯一元素,例如:div.masthead, div.logo
C:偽選擇器selectors
:lt(n): 查找哪些元素的同級索引值(它的位置在DOM樹中是相對于它的父節(jié)點)小于n脚曾,比如:td:lt(3) 表示小于三列的元素
:gt(n):查找哪些元素的同級索引值大于n东且,比如: div p:gt(2)表示哪些div中有包含2個以上的p元素
:eq(n): 查找哪些元素的同級索引值與n相等,比如:form input:eq(1)表示包含一個input標(biāo)簽的Form元素
:has(seletor): 查找匹配選擇器包含元素的元素本讥,比如:div:has(p)表示哪些div包含了p元素
:not(selector): 查找與選擇器不匹配的元素珊泳,比如: div:not(.logo) 表示不包含 class="logo" 元素的所有 div 列表
:contains(text): 查找包含給定文本的元素,搜索不區(qū)分大不寫拷沸,比如: p:contains(jsoup)
:containsOwn(text): 查找直接包含給定文本的元素
:matches(regex): 查找哪些元素的文本匹配指定的正則表達(dá)式色查,比如:div:matches((?i)login)
:matchesOwn(regex): 查找自身包含文本匹配指定正則表達(dá)式的元素
注意:上述偽選擇器索引是從0開始的,也就是
4.3撞芍、從元素抽取屬性秧了,本文和HTML
1)存在問題
在解析獲得一個Document實例對象,并查找到一些元素之后序无,你希望取得在這些元素中的數(shù)據(jù)验毡。
2)方法
要取得一個屬性的值,可以使用Node.attr(String key) 方法
對于一個元素中的文本帝嗡,可以使用Element.text()方法
對于要取得元素或?qū)傩灾械腍TML內(nèi)容晶通,可以使用Element.html(), 或 Node.outerHtml()方法
String html = "<p>An <a ><b>example</b></a> link.</p>";
Document doc = Jsoup.parse(html);//解析HTML字符串返回一個Document實現(xiàn)
Element link = doc.select("a").first();//查找第一個a元素
String text = doc.body().text(); // "An example link"http://取得字符串中的文本
String linkHref = link.attr("href"); // "http://example.com/"http://取得鏈接地址
String linkText = link.text(); // "example""http://取得鏈接地址中的文本
String linkOuterH = link.outerHtml(); // "<a
String linkInnerH = link.html(); // "<b>example</b>"http://取得鏈接內(nèi)的html內(nèi)容
3)說明
上述方法是元素數(shù)據(jù)訪問的核心辦法。此外還其它一些方法可以使用:
Element.id()
Element.tagName()
Element.className() and Element.hasClass(String className)
這些訪問器方法都有相應(yīng)的setter方法來更改數(shù)據(jù).
4.4丈探、處理URLs
1)存在問題
你有一個包含相對URLs路徑的HTML文檔录择,需要將這些相對路徑轉(zhuǎn)換成絕對路徑的URLs拔莱。
2)方法
在你解析文檔時確保有指定base URI碗降,然后
使用 abs: 屬性前綴來取得包含base URI的絕對路徑。代碼如下:
Document doc = Jsoup.connect("http://www.open-open.com").get();
Element link = doc.select("a").first();
String relHref = link.attr("href"); // == "/"
String absHref = link.attr("abs:href"); // "http://www.open-open.com/"
3)說明
在HTML元素中塘秦,URLs經(jīng)常寫成相對于文檔位置的相對路徑: <a href="/download">...</a>.
當(dāng)你使用 Node.attr(String key) 方法來取得a元素的href屬性時讼渊,它將直接返回在HTML源碼中指定定的值。
假如你需要取得一個絕對路徑尊剔,需要在屬性名前加 abs: 前綴爪幻。這樣就可以返回包含根路徑的URL地址attr("abs:href")
因此,在解析HTML文檔時须误,定義base URI非常重要挨稿。
如果你不想使用abs: 前綴,還有一個方法能夠?qū)崿F(xiàn)同樣的功能 Node.absUrl(String key)京痢。
4.5奶甘、實例程序:獲取所有連鏈接
1)說明
這個示例程序?qū)⒄故救绾螐囊粋€URL獲得一個頁面。然后提取頁面中的所有鏈接祭椰、圖片和其它輔助內(nèi)容臭家。并檢查URLs和文本信息疲陕。
2)運行下面程序需要執(zhí)行一個URLs作為參數(shù)
package org.jsoup.examples;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException; /**
* Example program to list links from a URL. */
public class ListLinks { public static void main(String[] args) throws IOException {
Validate.isTrue(args.length == 1, "usage: supply url to fetch");
String url = args[0];
print("Fetching %s...", url);
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
Elements media = doc.select("[src]");
Elements imports = doc.select("link[href]");
print("\nMedia: (%d)", media.size()); for (Element src : media) { if (src.tagName().equals("img"))
print(" * %s: <%s> %sx%s (%s)",
src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
trim(src.attr("alt"), 20)); else print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
}
print("\nImports: (%d)", imports.size()); for (Element link : imports) {
print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));
}
print("\nLinks: (%d)", links.size()); for (Element link : links) {
print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
}
} private static void print(String msg, Object... args) {
System.out.println(String.format(msg, args));
} private static String trim(String s, int width) { if (s.length() > width) return s.substring(0, width-1) + "."; else
return s;
}
}
3)查看結(jié)果
Fetching http://news.ycombinator.com/...
Media: (38) * img: <http://ycombinator.com/images/y18.gif> 18x18 ()
* img: <http://ycombinator.com/images/s.gif> 10x1 ()
* img: <http://ycombinator.com/images/grayarrow.gif> x ()
* img: <http://ycombinator.com/images/s.gif> 0x10 ()
* script: <http://www.co2stats.com/propres.php?s=1138>
* img: <http://ycombinator.com/images/s.gif> 15x1 ()
* img: <http://ycombinator.com/images/hnsearch.png> x ()
* img: <http://ycombinator.com/images/s.gif> 25x1 ()
* img: <http://mixpanel.com/site_media/images/mixpanel_partner_logo_borderless.gif> x (Analytics by Mixpan.)
Imports: (2) * link <http://ycombinator.com/news.css> (stylesheet)
* link <http://ycombinator.com/favicon.ico> (shortcut icon)
Links: (141) * a: <http://ycombinator.com> ()
* a: <http://news.ycombinator.com/news> (Hacker News)
* a: <http://news.ycombinator.com/newest> (new)
* a: <http://news.ycombinator.com/newcomments> (comments)
* a: <http://news.ycombinator.com/leaders> (leaders)
* a: <http://news.ycombinator.com/jobs> (jobs)
* a: <http://news.ycombinator.com/submit> (submit)
* a: <http://news.ycombinator.com/x?fnid=JKhQjfU7gW> (login)
* a: <http://news.ycombinator.com/vote?for=1094578&dir=up&whence=%6e%65%77%73> ()
* a: <http://www.readwriteweb.com/archives/facebook_gets_faster_debuts_homegrown_php_compiler.php?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+readwriteweb+%28ReadWriteWeb%29&utm_content=Twitter> (Facebook speeds up PHP)
* a: <http://news.ycombinator.com/user?id=mcxx> (mcxx)
* a: <http://news.ycombinator.com/item?id=1094578> (9 comments)
* a: <http://news.ycombinator.com/vote?for=1094649&dir=up&whence=%6e%65%77%73> ()
* a: <http://groups.google.com/group/django-developers/msg/a65fbbc8effcd914> ("Tough. Django produces XHTML.")
* a: <http://news.ycombinator.com/user?id=andybak> (andybak)
* a: <http://news.ycombinator.com/item?id=1094649> (3 comments)
* a: <http://news.ycombinator.com/vote?for=1093927&dir=up&whence=%6e%65%77%73> ()
* a: <http://news.ycombinator.com/x?fnid=p2sdPLE7Ce> (More)
* a: <http://news.ycombinator.com/lists> (Lists)
* a: <http://news.ycombinator.com/rss> (RSS)
* a: <http://ycombinator.com/bookmarklet.html> (Bookmarklet)
* a: <http://ycombinator.com/newsguidelines.html> (Guidelines)
* a: <http://ycombinator.com/newsfaq.html> (FAQ)
* a: <http://ycombinator.com/newsnews.html> (News News)
* a: <http://news.ycombinator.com/item?id=363> (Feature Requests)
* a: <http://ycombinator.com> (Y Combinator)
* a: <http://ycombinator.com/w2010.html> (Apply)
* a: <http://ycombinator.com/lib.html> (Library)
* a: <http://www.webmynd.com/html/hackernews.html> ()
* a: <http://mixpanel.com/?from=yc> ()
五、數(shù)據(jù)修改
5.1钉赁、設(shè)置屬性值
1)存在問題
在你解析一個Document之后可能想修改其中的某些屬性值蹄殃,然后再保存到磁盤或都輸出到前臺頁面。
2)方法
可以使用屬性設(shè)置方法 Element.attr(String key, String value), 和 Elements.attr(String key, String value).
假如你需要修改一個元素的 class 屬性你踩,可以使用 Element.addClass(String className) 和 Element.removeClass(String className) 方法诅岩。
Elements 提供了批量操作元素屬性和class的方法,比如:要為div中的每一個a元素都添加一個 rel="nofollow" 可以使用如下方法:
doc.select("div.comments a").attr("rel", "nofollow");
3)說明
與Element中的其它方法一樣带膜,attr 方法也是返回當(dāng) Element (或在使用選擇器是返回 Elements 集合)按厘。
這樣能夠很方便使用方法連用的書寫方式。比如:
doc.select("div.masthead").attr("title", "jsoup").addClass("round-box");
5.2钱慢、設(shè)置一個元素的HTML內(nèi)容
1)存在問題
你需要一個元素中的HTML的內(nèi)容
2)方法
可以使用Element中的HTML設(shè)置方法具體如下:
Element div = doc.select("div").first(); // <div></div>
div.html("<p>lorem ipsum</p>"); // <div><p>lorem ipsum</p></div>
div.prepend("<p>First</p>");//在div前添加html內(nèi)容
div.append("<p>Last</p>");//在div之后添加html內(nèi)容 // 添完后的結(jié)果: <div><p>First</p><p>lorem ipsum</p><p>Last</p></div>
Element span = doc.select("span").first(); // <span>One</span>
span.wrap("<li><a ); // 添完后的結(jié)果: <li><a ><span>One</span></a></li>
3)說明
Element.html(String html) 這個方法將先清除元素中的HTML內(nèi)容逮京,然后用傳入的HTML代替。
Element.prepend(String first) 和 Element.append(String last) 方法用于在分別在元素內(nèi)部HTML的前面和后面添加HTML內(nèi)容
Element.wrap(String around) 對元素包裹一個外部HTML內(nèi)容束莫。