使用 jsoup 解析HTML


// 參考資料:
    //  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]: 利用屬性值匹配正則表達式來查找元素,比如: 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): 查找哪些元素的文本匹配指定的正則表達式,比如:div:matches((?i)login)
:matchesOwn(regex): 查找自身包含文本匹配指定正則表達式的元素
注意:上述偽選擇器索引是從0開始的,也就是說第一個元素索引值為0,第二個元素index為1等
可以查看Selector API參考來了解更詳細的內容

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM