使用 jsoup 解析HTML

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   使用   for   sp   

// 參考資料:    //  http://www.jb51.net/article/43485.htm        @Test     public void  AnalysisHTMLByString()     {        String  html="<p><a href=\"a.html\">a</p><p> 文本</p>";        Document doc = Jsoup.parse(html);        Elements ele=doc.getElementsByTag("p");        for(Element e :ele)        {              System.out.println(e.text());                    }                    }        @Test    public void AnlysisHTMLByFile() throws IOException    {         File file=new File(System.getProperty("user.dir")+"\\a.html");         Document doc=Jsoup.parse(file, "UTF-8");         Elements eles=doc.getElementsByTag("a");         for(Element e :eles)         {               System.out.println(e.text());               System.out.println(e.attr("href"));         }        Element ele =doc.getElementById("btn");        System.out.println(ele.html());            }        @Test    public void  AnlysisHTMLByURL() throws IOException    {        int  timeout=3000;       Document doc=  Jsoup.connect("http://www.cnblogs.com/rhythmK/").get();              //擷取A標籤個數       System.out.println("共有超連結:"+ doc.getElementsByTag("a").size());              System.out.println("擷取指定ID:"+ doc.getElementById("navigator").html());               Elements eles= doc.select("#navigator");        for(Element ele :eles)        {            System.out.println(ele.html());        }    }

Elements這個對象提供了一系列類似於DOM的方法來尋找元素,抽取並處理其中的資料。具體如下:
尋找元素
getElementById(String id)
getElementsByTag(String tag)
getElementsByClass(String className)
getElementsByAttribute(String key) (and related methods)
Element siblings: siblingElements(), firstElementSibling(), lastElementSibling();nextElementSibling(), previousElementSibling()
Graph: parent(), children(), child(int index)
元素資料
attr(String key)擷取屬性attr(String key, String value)設定屬性
attributes()擷取所有屬性
id(), className() and classNames()
text()擷取常值內容text(String value) 設定常值內容
html()擷取元素內HTMLhtml(String value)設定元素內的HTML內容
outerHtml()擷取元素外HTML內容
data()擷取資料內容(例如:script和style標籤)
tag() and tagName()
操作HTML和文本
append(String html), prepend(String html)
appendText(String text), prependText(String text)
appendElement(String tagName), prependElement(String tagName)
html(String value)


Selector選取器概述
tagname: 通過標籤尋找元素,比如:a
ns|tag: 通過標籤在命名空間尋找元素,比如:可以用 fb|name 文法來尋找 <fb:name> 元素
#id: 通過ID尋找元素,比如:#logo
.class: 通過class名稱尋找元素,比如:.masthead
[attribute]: 利用屬性尋找元素,比如:[href]
[^attr]: 利用屬性名稱首碼來尋找元素,比如:可以用[^data-] 來尋找帶有HTML5 Dataset屬性的元素
[attr=value]: 利用屬性值來尋找元素,比如:[width=500]
[attr^=value], [attr$=value], [attr*=value]: 利用匹配屬性值開頭、結尾或包含屬性值來尋找元素,比如:[href*=/path/]
[attr~=regex]: 利用屬性值匹配Regex來尋找元素,比如: img[src~=(?i)\.(png|jpe?g)]
*: 這個符號將匹配所有元素
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標籤下所有直接子項目
siblingA + siblingB: 尋找在A元素之前第一個同級元素B,比如:div.head + div
siblingA ~ siblingX: 尋找A元素之前的同級X元素,比如:h1 ~ p
el, el, el:多個選取器組合,尋找匹配任一選取器的唯一元素,例如:div.masthead, div.logo
偽選取器selectors
:lt(n): 尋找哪些元素的同級索引值(它的位置在DOM樹中是相對於它的父節點)小於n,比如:td:lt(3) 表示小於三列的元素
:gt(n):尋找哪些元素的同級索引值大於n,比如: div p:gt(2)表示哪些div中有包含2個以上的p元素
:eq(n): 尋找哪些元素的同級索引值與n相等,比如:form input:eq(1)表示包含一個input標籤的Form元素
:has(seletor): 尋找匹配選取器包含元素的元素,比如:div:has(p)表示哪些div包含了p元素
:not(selector): 尋找與選取器不匹配的元素,比如: div:not(.logo) 表示不包含 class="logo" 元素的所有 div 列表
:contains(text): 尋找包含給定文本的元素,搜尋不區分大不寫,比如: p:contains(jsoup)
:containsOwn(text): 尋找直接包含給定文本的元素
:matches(regex): 尋找哪些元素的文本匹配指定的Regex,比如:div:matches((?i)login)
:matchesOwn(regex): 尋找自身包含文本匹配指定Regex的元素
注意:上述偽選取器索引是從0開始的,也就是說第一個元素索引值為0,第二個元素index為1等
可以查看Selector API參考來瞭解更詳細的內容

 

使用 jsoup 解析HTML

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.