# 從元素抽取屬性,文本和HTML
<div><h2>問題</h2>
<p>在解析獲得一個Document實例對象,并查找到一些元素之后,你希望取得在這些元素中的數據。</p>
<h2>方法</h2>
<ul>
<li>要取得一個屬性的值,可以使用<code><a title="Get an attribute's value by its key." href="http://jsoup.org/apidocs/org/jsoup/nodes/Node.html#attr%28java.lang.String%29">Node.attr(String
key)</a></code> 方法 </li>
<li>對于一個元素中的文本,可以使用<code><a title="Gets the combined text of this element and all its children." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#text%28%29">Element.text()</a></code>方法</li>
<li>對于要取得元素或屬性中的HTML內容,可以使用<code><a title="Retrieves the element's inner HTML." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#html%28%29">Element.html()</a></code>,
或 <code><a title="Get the outer HTML of this node." href="http://jsoup.org/apidocs/org/jsoup/nodes/Node.html#outerHtml%28%29">Node.outerHtml()</a></code>方法</li>
</ul>
<p>示例:</p>
<pre><code>String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
Document doc = Jsoup.parse(html);//解析HTML字符串返回一個Document實現
Element link = doc.select("a").first();//查找第一個a元素
String text = doc.body().text(); // "An example link"//取得字符串中的文本
String linkHref = link.attr("href"); // "http://example.com/"//取得鏈接地址
String linkText = link.text(); // "example""//取得鏈接地址中的文本
String linkOuterH = link.outerHtml();
// "<a href="http://example.com"><b>example</b></a>"
String linkInnerH = link.html(); // "<b>example</b>"//取得鏈接內的html內容
</code></pre>
<h2>說明</h2>
<p>上述方法是元素數據訪問的核心辦法。此外還其它一些方法可以使用:</p>
<ul>
<li><code><a title="Get the id attribute of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#id%28%29">Element.id()</a></code></li>
<li><code><a title="Get the name of the tag for this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#tagName%28%29">Element.tagName()</a></code></li>
<li><code><a title="Gets the literal value of this element's "class" attribute, which may include multiple class names, space separated." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#className%28%29">Element.className()</a></code>
and <code><a title="Tests if this element has a class." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#hasClass%28java.lang.String%29">Element.hasClass(String
className)</a></code></li></ul>
<p>這些訪問器方法都有相應的setter方法來更改數據.</p>
<h2>參見</h2>
<ul>
<li><code><a title="A HTML element consists of a tag name, attributes, and child nodes (including text nodes and other elements)." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html">Element</a></code>和<code><a title="A list of Elements, with methods that act on every element in the list." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html">Elements</a></code>集合類的參考文檔</li>
<li><a href="http://www.open-open.com/jsoup/working-with-urls.htm">URLs處理</a></li>
<li><a href="http://www.open-open.com/jsoup/selector-syntax.htm">使用CSS選擇器語法來查找元素</a></li></ul></div>
- Introduction
- 爬蟲相關技能介紹
- 爬蟲簡單介紹
- 爬蟲涉及到的知識點
- 爬蟲用途
- 爬蟲流程介紹
- 需求描述
- Http請求處理
- http基礎知識介紹
- http狀態碼
- httpheader
- java原生態處理http
- URL類
- 獲取URL請求狀態
- 模擬Http請求
- apache httpclient
- Httpclient1
- httpclient2
- httpclient3
- httpclient4
- httpclient5
- httpclient6
- okhttp
- OKhttp使用教程
- 技術使用
- java執行javascript
- 網頁解析
- Xpath介紹
- HtmlCleaner
- HtmlCleaner介紹
- HtmlCleaner使用
- HtmlParser
- HtmlParser介紹
- Jsoup
- 解析和遍歷一個HTML文檔
- 解析一個HTML字符串
- 解析一個body片斷
- 從一個URL加載一個Document
- 從一個文件加載一個文檔
- 使用DOM方法來遍歷一個文檔
- 使用選擇器語法來查找元素
- 從元素抽取屬性,文本和HTML
- 處理URLs
- 示例程序 獲取所有鏈接
- 設置屬性的值
- 設置一個元素的HTML內容
- 消除不受信任的HTML (來防止XSS攻擊)
- 正則表達式
- elasticsearch筆記
- 下載安裝elasticsearch
- 檢查es服務健康