# 使用DOM方法來遍歷一個文檔
<div><h2>問題</h2>
<p>你有一個HTML文檔要從中提取數據,并了解這個HTML文檔的結構。</p>
<h2>方法</h2>
<p>將HTML解析成一個<code><a title="A HTML Document." href="http://jsoup.org/apidocs/org/jsoup/nodes/Document.html">Document</a></code>之后,就可以使用類似于<abbr title="Document Object Model">DOM的方法</abbr>進行操作。示例代碼:</p>
<pre><code>File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}
</code></pre>
<h2>說明</h2>
<p>Elements這個對象提供了一系列類似于DOM的方法來查找元素,抽取并處理其中的數據。具體如下:</p>
<h3>查找元素</h3>
<ul>
<li><code><a title="Find an element by ID, including or under this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementById%28java.lang.String%29">getElementById(String
id)</a></code></li>
<li><code><a title="Finds elements, including and recursively under this element, with the specified tag name." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementsByTag%28java.lang.String%29">getElementsByTag(String
tag)</a></code></li>
<li><code><a title="Find elements that have this class, including or under this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementsByClass%28java.lang.String%29">getElementsByClass(String
className)</a></code></li>
<li><code><a title="Find elements that have a named attribute set." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementsByAttribute%28java.lang.String%29">getElementsByAttribute(String
key)</a></code> (and related methods)</li>
<li>Element siblings: <code><a title="Get sibling elements." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#siblingElements%28%29">siblingElements()</a></code>,
<code><a title="Gets the first element sibling of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#firstElementSibling%28%29">firstElementSibling()</a></code>,
<code><a title="Gets the last element sibling of this element" href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#lastElementSibling%28%29">lastElementSibling()</a></code>;
<code><a title="Gets the next sibling element of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#nextElementSibling%28%29">nextElementSibling()</a></code>,
<code><a title="Gets the previous element sibling of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#previousElementSibling%28%29">previousElementSibling()</a></code></li>
<li>Graph: <code><a title="Gets this node's parent node." href="http://jsoup.org/apidocs/org/jsoup/nodes/Node.html#parent%28%29">parent()</a></code>,
<code><a title="Get this element's child elements." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#children%28%29">children()</a></code>,
<code><a title="Get a child element of this element, by its 0-based index number." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#child%28int%29">child(int
index)</a></code></li></ul>
<h3>元素數據</h3>
<ul>
<li><code><a title="Get an attribute value from the first matched element that has the attribute." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#attr%28java.lang.String%29">attr(String
key)</a></code>獲取屬性<code><a title="Set an attribute on all matched elements." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#attr%28java.lang.String,%20java.lang.String%29">attr(String
key, String value)</a></code>設置屬性</li>
<li><code><a href="http://jsoup.org/apidocs/org/jsoup/nodes/TextNode.html#attributes%28%29">attributes()</a></code>獲取所有屬性</li>
<li><code><a title="Get the id attribute of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#id%28%29">id()</a></code>,
<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">className()</a></code>
and <code><a title="Get all of the element's class names." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#classNames%28%29">classNames()</a></code></li>
<li><code><a title="Get the combined text of all the matched elements." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#text%28%29">text()</a></code>獲取文本內容<code><a title="Set the text content of this text node." href="http://jsoup.org/apidocs/org/jsoup/nodes/TextNode.html#text%28java.lang.String%29">text(String
value)</a></code> 設置文本內容</li>
<li><code><a title="Get the combined inner HTML of all matched elements." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#html%28%29">html()</a></code>獲取元素內HTML<code><a title="Set the inner HTML of each matched element." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#html%28java.lang.String%29">html(String
value)</a></code>設置元素內的HTML內容</li>
<li><code><a title="Get the combined outer HTML of all matched elements." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#outerHtml%28%29">outerHtml()</a></code>獲取元素外HTML內容</li>
<li><code><a title="Get the combined data of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#data%28%29">data()</a></code>獲取數據內容(例如:script和style標簽)</li>
<li><code><a title="Get the Tag for this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#tag%28%29">tag()</a></code>
and <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">tagName()</a></code></li></ul>
<h3>操作HTML和文本</h3>
<ul>
<li><code><a title="Add the supplied HTML to the end of each matched element's inner HTML." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#append%28java.lang.String%29">append(String
html)</a></code>, <code><a title="Add the supplied HTML to the start of each matched element's inner HTML." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#prepend%28java.lang.String%29">prepend(String
html)</a></code></li>
<li><code><a title="Create and append a new TextNode to this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#appendText%28java.lang.String%29">appendText(String
text)</a></code>, <code><a title="Create and prepend a new TextNode to this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#prependText%28java.lang.String%29">prependText(String
text)</a></code></li>
<li><code><a title="Create a new element by tag name, and add it as the last child." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#appendElement%28java.lang.String%29">appendElement(String
tagName)</a></code>, <code><a title="Create a new element by tag name, and add it as the first child." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#prependElement%28java.lang.String%29">prependElement(String
tagName)</a></code></li>
<li><code><a title="Set the inner HTML of each matched element." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#html%28java.lang.String%29">html(String
value)</a></code></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服務健康