jsoup:解析HTML用法小結,jsouphtml用法小結
1.解析方式
(1)從字串解析
String html = "<html><head><title>First parse</title></head><body><p>Parse HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
?
(2)從URL擷取解析
Document doc = Jsoup.connect("http://example.com/").get();
String title = doc.title();
Document doc = Jsoup.connect("http://example.com") .data("query","Java").userAgent("Mozilla").cookie("auth","token").timeout(3000).post();
??
(3)從檔案解析
File input = newFile("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8","http://example.com/");
2.DOM方式遍曆元素
(1)搜尋元素
getElementById(String id)
getElementByTag(String tag)
getElementByClass(String className)
getElementByAttribute(String key)
siblingElements(), firstElementSibling(), lastElementSibling(), nextElementSibling(), previousElementSibling()
parent(), children(), child(
int
index)
(2)擷取元素資料
attr(String key) – 擷取key屬性
attributes() – 擷取屬性
id(), className(), classNames()
text() – 擷取常值內容
html() – 擷取元素內部HTML內容
outerHtml() – 擷取包括此元素的HTML內容
data() – 擷取<srcipt>或<style>標籤中的內容
tag(), tagName()
3.選取器文法(jsoup與其他解析器的區別就是可以使用類似jquery的選取器文法來搜尋及過濾出所需的元素)
(1)基本選取器
tagname: 搜尋tag標籤的元素
ns|tag: 搜尋命名空間內tag標籤的元素,如fb|name:<fb:name>
#id: 搜尋有指定id的元素
.
class
: 搜尋有指定
class
的元素
[attribute]: 搜尋有attrribute屬性的元素
[^attri]: 搜尋有以attri開頭的屬性的元素
[attr=value]: 搜尋有指定屬性及其屬性值的元素
[attr^=value], [attr$=value], [attr*=value]: 搜尋有指定attr屬性,且其屬性值是以value開頭、結尾或包括value的元素,如[href*=/path/]
[attr~=regex]: 搜尋有指定attr屬性,且其屬性值符合regexRegex的元素
*: 搜尋所有元素(2)選取器組合
el#id: 同時指定標籤名稱和id
el.
class
: 同時指定標籤名稱和
class
el[attr]: 同時指定標籤名稱和及其中所含屬性的名稱
上述
3
項的任意組合,如a[href].highlight
ancestor child: 包含,如div.content p,即搜尋<div
class
=”content”>下含有<p>標籤的元素
ancestor > child: 直接包含,如div.content > p,即搜尋直屬<div
class
=
"content"
>節點下的<p>標籤元素;div.content > *,即搜尋<div
class
=
"content"
>下的所有元素
siblingA + siblingB: 直接遍曆,如div.head + div,即搜尋<div
class
=
"head"
><div>的元素,其中不再包含子項目
siblingA ~ siblingX: 遍曆,如h1 ~ p,即<h1>下直接或間接有<p>的元素
el, el, el: 組合多個選取器,搜尋滿足其中一個選取器的元素(3)偽選取器(條件選取器)
:lt(n): 搜尋n號元素之前的元素
:gt(n): 搜尋n號元素之後的元素
:eq(n): 搜尋n號元素
:has(seletor): 搜尋符合指定選取器的元素
:not(seletor): 搜尋不符合指定選取器的元素
:contains(text): 搜尋包含指定文本的元素,區分大小寫
:containsOwn(text): 搜尋直接指包含指定文本的元素
:matches(regex): 搜尋符合指定Regex的元素
:matchesOwn(regex): 搜尋本元素文本中符合指定Regex的元素
注意:以上偽選取器的索引中,第一個元素位於索引
0
,第二個元素位於索引
1
,……4.擷取元素的屬性、文本和HTML
擷取元素的屬性值:Node.attr(String key)
擷取元素的文本,包括與其組合的子項目:Element.text()
擷取HTML:Element.html()或Node.outerHtml()5.操作URL
Element.attr(
"href"
) – 直接擷取URL
Element.attr(
"abs:href"
)或Element.absUrl(
"href"
) – 擷取完整URL。如果HTML是從檔案或字串解析過來的,需要調用Jsoup.setBaseUri(String baseUri)來指定基URL,否則擷取的完整URL只會是Null 字元串6.測試例子
li[
class
=info] a[
class
=Author] - 空格前後表示內含項目關聯性,即表示li裡的a
div[
class
=mod mod-main mod-lmain]:contains(教學反思) - div中包含
"教學反思"
,適合約時有多個同名DIV的情況
/*
previousSibling()擷取某標籤前面的代碼
nextSibling()擷取某標籤後的代碼
如:
<form id=form1>
第一名:Lily <br/>
第二名:Tom <br/>
第三名:Peter <br/>
</form>
*/
Elements items = doc.select(
"form[id=form1]"
);
Elements prevs = items.select(
"br"
);
for
(Element p : prevs){
String prevStr = p.previousSibling().toString().trim());
}
/*
最常用的連結抓取
*/
String itemTag =
"div[class=mydiv]"
;
String linkTag =
"a"
Elements items = doc.select(itemTag);
Elements links = items.select(linkTag);
for
(Element l : links){
String href = l.attr(
"abs:href"
);
//完整Href
String absHref = l.attr(
"href"
);
//相對路徑
String text = l.text();
String title = l.attr(
"title"
);
}7.jsoup線上API
http://jsoup.org/apidocs/