1.解析方式 
(1)从字符串解析 

?



1



2



3


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


?



1



2


Document doc = Jsoup.connect( "http://example.com/" ).get();



String title = doc.title();



?



1



2



3



4



5



6


Document doc = Jsoup.connect( "http://example.com" )



.data( "query" , "Java" )



.userAgent( "Mozilla" )



.cookie( "auth" , "token" )



.timeout( 3000 )



.post();


(3)从文件解析


?



1



2


File input = new File( "/tmp/input.html" );



Document doc = Jsoup.parse(input, "UTF-8" , "http://example.com/" );


2.DOM方式遍历元素


(1)搜索元素


?



1



2



3



4



5



6


getElementById(String id)



getElementByTag(String tag)



getElementByClass(String className)



getElementByAttribute(String key)



siblingElements(), firstElementSibling(), lastElementSibling(), nextElementSibling(), previousElementSibling()



parent(), children(), child( int index)


(2)获取元素数据


?



1



2



3



4



5



6



7



8


attr(String key) – 获取key属性



attributes() – 获取属性



id(), className(), classNames()



text() – 获取文本内容



html() – 获取元素内部HTML内容



outerHtml() – 获取包括此元素的HTML内容



data() – 获取<srcipt>或<style>标签中的内容



tag(), tagName()


3.选择器语法(jsoup与其他解析器的区别就是可以使用类似jquery的选择器语法来搜索及过滤出所需的元素)


(1)基本选择器


?



1



2



3



4



5



6



7



8



9



10


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属性,且其属性值符合regex正则表达式的元素



*: 搜索所有元素


(2)选择器组合


?



1



2



3



4



5



6



7



8



9


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)伪选择器(条件选择器)


?



1



2



3



4



5



6



7



8



9



10


:lt(n): 搜索n号元素之前的元素



:gt(n): 搜索n号元素之后的元素



:eq(n): 搜索n号元素



:has(seletor): 搜索符合指定选择器的元素



:not(seletor): 搜索不符合指定选择器的元素



:contains(text): 搜索包含指定文本的元素,区分大小写



:containsOwn(text): 搜索直接指包含指定文本的元素



:matches(regex): 搜索符合指定正则表达式的元素



:matchesOwn(regex): 搜索本元素文本中符合指定正则表达式的元素



注意:以上伪选择器的索引中,第一个元素位于索引 0 ,第二个元素位于索引 1 ,……


4.获取元素的属性、文本和HTML


?



1



2



3


获取元素的属性值:Node.attr(String key)



获取元素的文本,包括与其组合的子元素:Element.text()



获取HTML:Element.html()或Node.outerHtml()




5.操作URL


?



1



2


Element.attr( "href" ) – 直接获取URL



Element.attr( "abs:href" )或Element.absUrl( "href" ) – 获取完整URL。如果HTML是从文件或字符串解析过来的,需要调用Jsoup.setBaseUri(String baseUri)来指定基URL,否则获取的完整URL只会是空字符串



6.测试例子


?



1



2


li[ class =info] a[ class =Author] - 空格前后表示包含关系,即表示li里的a



div[ class =mod mod-main mod-lmain]:contains(教学反思) - div中包含 "教学反思" ,适合同时有多个同名DIV的情况



?



1



2



3



4



5



6



7



8



9



10



11



12



13



14



15


/*



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



}



?



1



2



3



4



5



6



7



8



9



10



11



12



13


/*



最常用的链接抓取



*/



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