jsoup 的基本使用以及API内容
Jsoup 獲取 Document 的三種方法:
1. 讀取取字符串的方式獲取 Document,代碼如下所示:
@Test //獲取完整的字符串內容 public void test01() {String html = "<html> <div> <h1>h1標簽</h1> <span>span標簽</span> </div> </html>";Document document = Jsoup.parse(html);System.out.println(document); }@Test //獲取部分的字符串內容 public void test02() {String html = "<html> <div> <h1>h1標簽</h1> <span>span標簽</span> </div> </html>";Document document = Jsoup.parse(html);//此處就像 JavaScript 的選擇器Elements div = document.getElementsByTag("div");System.out.println(div); }test01 輸出結果如下:
<html> <head></head><body><div> <h1>h1標簽</h1> <span>span標簽</span> </div> </body> </html>test02 輸出結果如下:
<div> <h1>h1標簽</h1> <span>span標簽</span> </div>2. 使用 GET 或則 POST 方式獲取 Document,代碼如下所示:
@Test //通過 GET 方式獲取 public void test03() {Document document;try {//get 請求System.out.println("GET 請求方式獲取數據");document = Jsoup.connect("http://bp.shijialeya.top").get();System.out.println(document);System.out.println();} catch (IOException e) {e.printStackTrace();} }@Test //通過 POST 方式獲取 public void test04() {Document document;try {//POST請求System.out.println("POST 請求方式獲取數據");document = Jsoup.connect("http://bp.shijialeya.top").data("username", "jiale") //添加請求數據參數。.userAgent("Mozilla AppleWebKit Chrome Safari Edg") //設置請求用戶代理頭。.cookie("auth", "token") //設置請求中要發送的 cookie。.timeout(2000) //設置超時時間.post();//更多的 API查看文章末尾 Connection 接口的方法System.out.println(document);} catch (IOException e) {e.printStackTrace();} }test03 輸出結果如下:
GET 請求方式獲取數據 <!doctype html> <html lang="ZH-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> //省略...test04 輸出結果如下:
POST 請求方式獲取數據 <!doctype html> <html lang="ZH-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> //省略...3. 在文件中獲取 Document,代碼如下所示:
在同目錄下創建 index.html
編寫 Java 類
@Test //通過 File 方式獲取 public void test05() {File file = new File("src/main/resources/index.html");Document document = null;try {document = Jsoup.parse(file, "UTF-8");System.out.println(document);} catch (IOException e) {e.printStackTrace();} finally {if (document != null){document.clone();}} }test05 輸出結果如下:
<html> <head> <title>hello</title> </head> <body>鍵盤敲爛, 工資過萬 </body> </html>使用 Document 通過選擇器獲取 Elements
常用的方法:
- select?(String cssQuery):查找與SelectorCSS查詢匹配的元素,類似于 JQuery 的選擇器。
- getElementById?(String id):通過ID查找元素
- getElementsByClass?(String className):通過class查找元素
- getElementsByTag?(String tagName) 通過指定名稱查找元素,并遞歸地查找這些元素。
- getAllElements():在此元素下找到所有元素。
- firstElementSibling():獲取此元素的第一個元素同級
- getElementsByAttributeStarting?(String keyPrefix):查找具有以提供的前綴開頭的屬性名稱的元素
- getElementsByAttributeValue?(String key, String value): 查找具有具有特定值的屬性的元素
- getElementsByAttributeValueContaining?(String key, String match):查找具有其值包含匹配字符串的屬性的元素
- getElementsByAttributeValueStarting?(String key, String valuePrefix):查找具有以值前綴開頭的屬性的元素
- getElementsByAttributeValueEnding?(String key, String valueSuffix):查找具有以值后綴結尾的屬性的元素
- getElementsContainingText?(String searchText) :查找包含指定字符串的元素
創建一個 index.html 文件,內容如下:
<html> <head><title>hello頁面</title> </head> <body><div class="boxClass">class div 盒子</div><div id="boxId">Id div 盒子</div><div><span>div 下的 span 標簽</span></div> </body> </html>Java 代碼示例:
@Test //選擇器 public void test06() {File file = new File("src/main/resources/index.html");Document document = null;try {document = Jsoup.parse(file, "UTF-8");//select 類似于 JQuery 選擇器Elements e1 = document.select("body>div>span"); //<span>div 下的 span 標簽</span>System.out.println(e1);//getElementById id 選擇器Element e2 = document.getElementById("boxId");System.out.println(e2);//<div id="boxId"> Id div 盒子 </div>//getElementsByClass class 選擇器Elements e3 = document.getElementsByClass("boxClass");System.out.println(e3);//<div class="boxClass"> class div 盒子 </div>//getAllElementsElements e4 = document.getElementsByTag("span");System.out.println(e4);//<span>div 下的 span 標簽</span>//...} catch (IOException e) {e.printStackTrace();} finally {if (document != null) {document.clone();}} }獲取 Elements 的內容
常用的方法:
- html():檢索元素的內部HTML。
- html?(String html):設置此元素的內部HTML。
- text():獲取此元素及其所有子元素的組合文本。
- text?(String text):設置文檔的文本。
Java 代碼示例:
@Test //獲取 Elements 的內容 public void test07() {File file = new File("src/main/resources/index.html");Document document = null;try {document = Jsoup.parse(file, "UTF-8");//html:不帶參數是獲取 html,帶參數是設置 htmlString html = document.select("body").html();System.out.println(html);//text:不帶參數是獲取 text,帶參數是設置 textString text = document.select("body").text();System.out.println(text);//class div 盒子 Id div 盒子 div 下的 span 標簽} catch (IOException e) {e.printStackTrace();} finally {if (document != null) {document.clone();}} }API
下面是 Jsoup 的 API,參考自:jsoup官方文檔(Jsoup、Document、Element 、Node、Elements、Connection 接口)
public class org.jsoup.Jsoup 類
extends Object
所有方法
| static String | clean?(String bodyHtml, String baseUri, Whitelist whitelist) | 通過解析輸入HTML并通過允許的標簽和屬性的白名單對其進行過濾,從不受信任的輸入HTML中獲得安全的HTML。 |
| static String | clean?(String bodyHtml, String baseUri, Whitelist whitelist, Document.OutputSettings outputSettings) | 通過解析輸入HTML并通過允許的標簽和屬性的白名單對其進行過濾,從不受信任的輸入HTML中獲得安全的HTML。 |
| static String | clean?(String bodyHtml, Whitelist whitelist) | 通過解析輸入HTML并通過允許的標簽和屬性的白名單對其進行過濾,從不受信任的輸入HTML中獲得安全的HTML。 |
| static Connection | connect?(String url) | 創建一個新Connection的URL。 |
| static boolean | isValid?(String bodyHtml, Whitelist whitelist) | 測試輸入正文HTML是否僅具有白名單允許的標簽和屬性。 |
| static Document | parse?(File in, String charsetName) | 將文件內容解析為HTML。 |
| static Document | parse?(File in, String charsetName, String baseUri) | 將文件內容解析為HTML。 |
| static Document | parse?(InputStream in, String charsetName, String baseUri) | 讀取輸入流,并將其解析為Document。 |
| static Document | parse?(InputStream in, String charsetName, String baseUri, Parser parser) | 讀取輸入流,并將其解析為Document。 |
| static Document | parse?(String html) | 將HTML解析為文檔。 |
| static Document | parse?(String html, String baseUri) | 將HTML解析為文檔。 |
| static Document | parse?(String html, String baseUri, Parser parser) | 使用提供的解析器將HTML解析為文檔。 |
| static Document | parse?(URL url, int timeoutMillis) | 提取一個URL,并將其解析為HTML。 |
| static Document | parseBodyFragment?(String bodyHtml) | 假設HTML構成HTML的片段,則解析HTML的片段body。 |
| static Document | parseBodyFragment?(String bodyHtml, String baseUri) | 假設HTML構成HTML的片段,則解析HTML的片段body。 |
public class org.jsoup.nodes.Document 類
extends Element
構造方法
| Document?(String baseUri) | 創建一個新的空文檔。 |
所有方法
| Element | body() | 文檔body元素的訪問者。 |
| Charset | charset() | 返回此文檔中使用的字符集。 |
| void | charset?(Charset charset) | 設置本文檔中使用的字符集。 |
| Document | clone() | 創建該節點及其所有子節點的獨立的深層副本。 |
| Element | createElement?(String tagName) | 使用此文檔的基本uri創建一個新的Element。 |
| static | Document createShell?(String baseUri) | 創建一個有效的文檔空外殼,適用于向其中添加更多元素。 |
| DocumentType | documentType() | 返回此文檔的文檔類型。 |
| Element | head() | 文檔head元素的訪問者。 |
| String | location() | 獲取解析此文檔的URL。 |
| String | nodeName() | 獲取此節點的節點名稱。 |
| Document | normalise() | 規范化文檔。 |
| String | outerHtml() | 獲取此節點的外部HTML。 |
| Document.OutputSettings | outputSettings() | 獲取文檔的當前輸出設置。 |
| Document | outputSettings?(Document.OutputSettings outputSettings) | 設置文檔的輸出設置。 |
| Parser | parser() | 獲取用于解析此文檔的解析器。 |
| Document | parser?(Parser parser) | 設置用于創建此文檔的解析器。 |
| Document.QuirksMode | quirksMode() | |
| Document | quirksMode?(Document.QuirksMode quirksMode) | |
| Element | text?(String text) | 設置body本文檔的文本。 |
| String | title() | 獲取文檔title元素的字符串內容。 |
| void | title?(String title) | 設置文檔的title元素。 |
| boolean | updateMetaCharsetElement() | 返回文檔中具有字符集信息的元素是否在通過document .charset(charset)進行更改時被更新。 |
| void | updateMetaCharsetElement?(boolean update) | 設置當通過document .charset(charset)進行更改時,此文檔中具有字符集信息的元素是否被更新。 |
| Document 繼承了 Element,包含了所有的 Element 的方法! |
public class org.jsoup.nodes.Element 類
extends Node
構造方法
| Element (String tag) | 創建一個新的獨立元素。 |
| Element (Tag tag, String baseUri) | 從標簽和基本URI創建一個新元素。 |
| Element? (Tag tag, String baseUri, Attributes attributes) | 創建一個新的獨立元素。 |
所有方法
| Element | addClass?(String className) | 在該元素的class屬性中添加一個類名。 |
| Element | after?(String html) | 將指定的HTML插入到此元素之后的DOM中(如下所示)。 |
| Element | after?(Node node) | 將指定的節點插入到該節點之后的DOM中(如下所示)。 |
| Element | append?(String html) | 向此元素添加內部HTML。 |
| Element | appendChild?(Node child) | 將節點子節點添加到此元素。 |
| Element | appendElement?(String tagName) | 通過標簽名稱創建一個新元素,并將其添加為最后一個子元素。 |
| Element | appendText?(String text) | 創建一個新的TextNode并將其附加到此元素。 |
| Element | appendTo?(Element parent) | 將此元素添加到提供的父元素中,作為其下一個子元素。 |
| Element | attr?(String attributeKey, boolean attributeValue) | 在此元素上設置布爾屬性值。 |
| Element | attr?(String attributeKey, String attributeValue) | 在此元素上設置屬性值。 |
| Attributes | attributes() | 獲取元素的所有屬性。 |
| String | baseUri() | 獲取適用于此節點的基本URI。 |
| Element | before?(String html) | 將指定的HTML插入此元素之前的DOM中(作為前面的同級元素)。 |
| Element | before?(Node node) | 將指定的節點插入到該節點之前的DOM中(作為先前的同級節點)。 |
| Element | child?(int index) | 通過從0開始的索引號獲取此元素的子元素。 |
| int | childNodeSize() | 獲取此節點擁有的子節點數。 |
| Elements | children() | 獲取此元素的子元素。 |
| int | childrenSize() | 獲取作為元素的該元素的子節點數。 |
| String | className() | 獲取此元素的“class”屬性的文字值,其中可能包含多個類名,以空格分隔。 |
| Set<String> | classNames() | 獲取所有元素的類名。 |
| Element | classNames?(Set<String> classNames) | 將元素的class屬性設置為提供的類名稱。 |
| Element | clearAttributes() | 清除(刪除)此節點中的所有屬性。 |
| Element | clone() | 創建該節點及其所有子節點的獨立的深層副本。 |
| Element | closest?(String cssQuery) | 在與指定的CSS查詢匹配的父級樹中找到最接近的元素。 |
| Element | closest?(Evaluator evaluator) | 在與指定評估者匹配的父級樹中找到最接近的元素。 |
| String | cssSelector() | 獲取將唯一選擇此元素的CSS選擇器。 |
| String | data() | 獲取此元素的組合數據。 |
| List<DataNode> | dataNodes() | 獲取此元素的子數據節點。 |
| Map<String,?String> | dataset() | 獲取此元素的HTML5自定義數據屬性。 |
| protected Element | doClone?(Node parent) | |
| protected void | doSetBaseUri?(String baseUri) | 如果此節點跟蹤基本URI,則僅為此節點(而不是其后代)設置baseUri。 |
| int | elementSiblingIndex() | 獲取此元素在其同級元素列表中的列表索引。 |
| Element | empty() | 刪除元素的所有子節點。 |
| protected List<Node> | ensureChildNodes() | |
| Element | filter?(NodeFilter nodeFilter) | 通過該節點及其后代執行深度優先過濾。 |
| Element | firstElementSibling() | 獲取此元素的第一個元素同級。 |
| Elements | getAllElements() | 在此元素下找到所有元素(包括自我和孩子的孩子)。 |
| Element | getElementById?(String id) | 通過ID查找元素,包括該元素或在該元素下。 |
| Elements | getElementsByAttribute?(String key) | 查找具有命名屬性集的元素。 |
| Elements | getElementsByAttributeStarting?(String keyPrefix) | 查找具有以提供的前綴開頭的屬性名稱的元素。 |
| Elements | getElementsByAttributeValue?(String key, String value) | 查找具有具有特定值的屬性的元素。 |
| Elements | getElementsByAttributeValueContaining?(String key, String match) | 查找具有其值包含匹配字符串的屬性的元素。 |
| Elements | getElementsByAttributeValueEnding?(String key, String valueSuffix) | 查找具有以值后綴結尾的屬性的元素。 |
| Elements | getElementsByAttributeValueMatching?(String key, String regex) | 查找具有其值與提供的正則表達式匹配的屬性的元素。 |
| Elements | getElementsByAttributeValueMatching?(String key, Pattern pattern) | 查找具有其值與提供的正則表達式匹配的屬性的元素。 |
| Elements | getElementsByAttributeValueNot?(String key, String value) | 查找不具有此屬性或具有不同值的元素。 |
| Elements | getElementsByAttributeValueStarting?(String key, String valuePrefix) | 查找具有以值前綴開頭的屬性的元素。 |
| Elements | getElementsByClass?(String className) | 查找具有此類的元素,包括該元素或在此元素下。 |
| Elements | getElementsByIndexEquals?(int index) | 查找其兄弟索引等于提供的索引的元素。 |
| Elements | getElementsByIndexGreaterThan?(int index) | 查找兄弟索引大于提供的索引的元素。 |
| Elements | getElementsByIndexLessThan?(int index) | 查找兄弟索引小于提供的索引的元素。 |
| Elements | getElementsByTag?(String tagName) | 查找具有指定標簽名稱的元素(包括在該元素下并在此元素下遞歸)。 |
| Elements | getElementsContainingOwnText?(String searchText) | 查找直接包含指定字符串的元素。 |
| Elements | getElementsContainingText?(String searchText) | 查找包含指定字符串的元素。 |
| Elements | getElementsMatchingOwnText?(String regex) | 查找其文本與提供的正則表達式匹配的元素。 |
| Elements | getElementsMatchingOwnText?(Pattern pattern) | 查找其文本與提供的正則表達式匹配的元素。 |
| Elements | getElementsMatchingText?(String regex) | 查找其文本與提供的正則表達式匹配的元素。 |
| Elements | getElementsMatchingText?(Pattern pattern) | 查找其文本與提供的正則表達式匹配的元素。 |
| protected boolean | hasAttributes() | 檢查此Node是否具有實際的Attributes對象。 |
| boolean | hasClass?(String className) | 測試此元素是否具有類。 |
| boolean | hasText() | 測試此元素是否具有任何文本內容(不只是空格)。 |
| String | html() | 檢索元素的內部HTML。 |
| Element | html?(String html) | 設置此元素的內部HTML。 |
| <T extends Appendable> | T html?(T appendable) | 將此節點及其子節點寫入給定的Appendable。 |
| String | id() | 獲取id此元素的屬性。 |
| Element | insertChildren?(int index, Collection<? extends Node> children) | 將給定的子節點插入到此元素的指定索引處。 |
| Element | insertChildren?(int index, Node… children) | 將給定的子節點插入到此元素的指定索引處。 |
| boolean | is?(String cssQuery) | 檢查此元素是否與給定的SelectorCSS查詢匹配。 |
| boolean | is?(Evaluator evaluator) | 檢查此元素是否與給定的評估者匹配。 |
| boolean | isBlock() | 測試此元素是否為塊級元素。 |
| Element | lastElementSibling() | 獲取此元素的最后一個元素同級 |
| Element | nextElementSibling() | 獲取此元素的下一個同級元素。 |
| Elements | nextElementSiblings() | 獲取此元素之后的每個同級元素。 |
| String | nodeName() | 獲取此節點的節點名稱。 |
| String | normalName() | 獲取此元素標簽的標準化名稱。 |
| String | ownText() | 僅獲取此元素擁有的文本;無法獲得所有子項的合并文本。 |
| Element | parent() | 獲取此節點的父節點。 |
| Elements | parents() | 獲取此元素的父項和祖先,直到文檔根目錄為止。 |
| Element | prepend?(String html) | 將內部HTML添加到此元素中。 |
| Element | prependChild?(Node child) | 在此元素的子元素的開頭添加一個節點。 |
| Element | prependElement?(String tagName) | 通過標簽名稱創建一個新元素,并將其添加為第一個子元素。 |
| Element | prependText?(String text) | 創建一個新的TextNode并將其添加到此元素之前。 |
| Element | previousElementSibling() | 獲取此元素的上一個元素同級。 |
| Elements | previousElementSiblings() | 獲取此元素之前的每個元素同級。 |
| Element | removeAttr?(String attributeKey) | 從此節點刪除屬性。 |
| Element | removeClass?(String className) | 從該元素的class屬性中刪除類名稱。 |
| Element | root() | 獲取此節點的根節點;就是它的最高祖先。 |
| Elements | select?(String cssQuery) | 查找與SelectorCSS查詢匹配的元素,并以該元素為起始上下文。 |
| Elements | select?(Evaluator evaluator) | 查找與提供的評估程序匹配的元素。 |
| Element | selectFirst?(String cssQuery) | 查找與SelectorCSS查詢匹配的第一個元素,并以該元素為起始上下文。 |
| Element | selectFirst?(Evaluator evaluator) | 查找與提供的Evaluator匹配的第一個元素,以該元素作為起始上下文,或者null如果沒有匹配項。 |
| Element | shallowClone() | 創建該節點的獨立淺副本。 |
| Elements | siblingElements() | 獲取同級元素。 |
| Tag | tag() | 獲取此元素的標簽。 |
| String | tagName() | 獲取此元素的標簽名稱。 |
| Element | tagName?(String tagName) | 更改此元素的標簽。 |
| String | text() | 獲取此元素及其所有子元素的組合文本。 |
| Element | text?(String text) | 設置此元素的文本。 |
| List<TextNode> | textNodes() | 獲取此元素的子文本節點。 |
| Element | toggleClass?(String className) | 在該元素的class屬性上切換類名稱:如果存在,將其刪除;否則添加它。 |
| Element | traverse?(NodeVisitor nodeVisitor) | 在該節點及其后代中進行深度優先遍歷。 |
| String | val() | 獲取表單元素的值(輸入,文本區域等)。 |
| Element | val?(String value) | 設置表單元素的值(輸入,文本區域等)。 |
| String | wholeText() | 獲取此元素的所有子元素的(未編碼)文本,包括原始元素中存在的所有換行符和空格。 |
| Element | wrap?(String html) | 將提供的HTML環繞此元素。 |
| Element 繼承 Node,包含了所有的 Node 的方法! |
public abstract class org.jsoup.nodes.Node 類
extends Object
implements Cloneable
所有方法
| String | absUrl?(String attributeKey) | 從可能是相對的URL屬性獲取絕對URL。 |
| protected void | addChildren?(int index, Node… children) | |
| protected void | addChildren?(Node… children) | |
| Node | after?(String html) | 將指定的HTML插入此節點之后的DOM中。 |
| Node | after?(Node node) | 將指定的節點插入此節點之后的DOM中。 |
| String | attr?(String attributeKey) | 通過其鍵獲取屬性的值。 |
| Node | attr?(String attributeKey, String attributeValue) | 設置屬性(鍵=值)。 |
| abstract Attributes | attributes() | 獲取元素的所有屬性。 |
| abstract String | baseUri() | 獲取適用于此節點的基本URI。 |
| Node | before?(String html) | 將指定的HTML插入此節點之前的DOM中。 |
| Node | before?(Node node) | 將指定節點插入該節點之前的DOM中。 |
| Node | childNode?(int index) | 通過基于0的索引獲取子節點。 |
| List<Node> | childNodes() | 獲取此節點的子級。 |
| protected Node[] | childNodesAsArray() | |
| List<Node> | childNodesCopy() | 返回此節點的子級的深層副本。 |
| abstract int | childNodeSize() | 獲取此節點擁有的子節點數。 |
| Node | clearAttributes() | 清除(刪除)此節點中的所有屬性。 |
| Node | clone() | 創建該節點及其所有子節點的獨立的深層副本。 |
| protected Node | doClone?(Node parent) | |
| protected abstract void | doSetBaseUri?(String baseUri) | 如果此節點跟蹤基本URI,則僅為此節點(而不是其后代)設置baseUri。 |
| abstract Node | empty() | 刪除該節點的所有子節點。 |
| protected abstract List<Node> | ensureChildNodes() | |
| boolean | equals?(Object o) | 檢查此節點是否與另一個節點相同(對象身份測試)。 |
| Node | filter?(NodeFilter nodeFilter) | 通過該節點及其后代執行深度優先過濾。 |
| boolean | hasAttr?(String attributeKey) | 測試此元素是否具有屬性。 |
| protected abstract boolean | hasAttributes() | 檢查此Node是否具有實際的Attributes對象。 |
| boolean | hasParent() | |
| boolean | hasSameValue?(Object o) | 檢查此節點是否與另一個節點具有相同的內容。 |
| <T extends Appendable> T | html?(T appendable) | 將此節點及其子節點寫入給定的Appendable。 |
| protected void | indent?(Appendable accum, int depth, Document.OutputSettings out) | |
| Node | nextSibling() | 獲取此節點的下一個同級。 |
| abstract String | nodeName() | 獲取此節點的節點名稱。 |
| String | outerHtml() | 獲取此節點的外部HTML。 |
| protected void | outerHtml?(Appendable accum) | |
| Document | ownerDocument() | 獲取與此節點關聯的文檔。 |
| Node | parent() | 獲取此節點的父節點。 |
| Node | parentNode() | 獲取此節點的父節點。 |
| Node | previousSibling() | 獲取此節點的先前同級。 |
| void | remove() | 從DOM樹中刪除(刪除)此節點。 |
| Node | removeAttr?(String attributeKey) | 從此節點刪除屬性。 |
| protected void | removeChild?(Node out) | |
| protected void | reparentChild?(Node child) | |
| protected void | replaceChild?(Node out, Node in) | |
| void | replaceWith?(Node in) | 用提供的節點替換DOM中的該節點。 |
| Node | root() | 獲取此節點的根節點;就是它的最高祖先。 |
| void | setBaseUri?(String baseUri) | 更新此節點及其所有后代的基本URI。 |
| protected void | setParentNode?(Node parentNode) | |
| protected void | setSiblingIndex?(int siblingIndex) | |
| Node | shallowClone() | 創建該節點的獨立淺副本。 |
| int | siblingIndex() | 獲取此節點在其節點同級列表中的列表索引。 |
| List<Node> | siblingNodes() | 檢索此節點的兄弟節點。 |
| String | toString() | 獲取此節點的外部HTML。 |
| Node | traverse?(NodeVisitor nodeVisitor) | 在該節點及其后代中進行深度優先遍歷。 |
| Node | unwrap() | 從DOM中刪除此節點,并將其子級上移到該節點的父級中。 |
| Node | wrap?(String html) | 將提供的HTML環繞此節點。 |
public class org.jsoup.select.Elements 類
extends ArrayList
構造方法
| Elements() | |
| Elements?(int initialCapacity) | |
| Elements?(Collection<Element> elements) | |
| Elements?(List<Element> elements) | |
| Elements?(Element… elements) |
所有方法
| String | absUrl?(String attributeKey) | 從可能是相對的URL屬性獲取絕對URL。 |
| Elements | addClass?(String className) | 將類名稱添加到每個匹配元素的class屬性中。 |
| Elements | after?(String html) | 在每個匹配元素的外部HTML之后插入提供的HTML。 |
| Elements | append?(String html) | 將提供的HTML添加到每個匹配元素的內部HTML的末尾。 |
| String | attr?(String attributeKey) | 從具有該屬性的第一個匹配元素中獲取一個屬性值。 |
| Elements | attr?(String attributeKey, String attributeValue) | 在所有匹配的元素上設置屬性。 |
| Elements | before?(String html) | 在每個匹配元素的外部HTML之前插入提供的HTML。 |
| Elements | clone() | 創建這些元素的深層副本。 |
| List<Comment> | comments() | 獲取Comment作為所選元素的直接子節點的節點。 |
| List<DataNode> | dataNodes() | 獲取DataNode作為所選元素的直接子節點的節點。 |
| List<String> | eachAttr?(String attributeKey) | 獲取每個匹配元素的屬性值。 |
| List<String> | eachText() | 獲取每個匹配元素的文本內容。 |
| Elements | empty() | 清空(從中刪除所有子節點)每個匹配的元素。 |
| Elements | eq?(int index) | 獲取第n個匹配的元素作為Elements對象。 |
| Elements | filter?(NodeFilter nodeFilter) | 對每個選定元素執行深度優先過濾。 |
| Element | first() | 獲取第一個匹配的元素。 |
| List<FormElement> | forms() | FormElement從所選元素(如果有)中 |
| boolean | hasAttr?(String attributeKey) | 檢查是否有任何匹配的元素定義了此屬性。 |
| boolean | hasClass?(String className) | 確定是否有任何匹配的元素在其class屬性中設置了此類名稱。 |
| boolean | hasText() | 測試是否有任何匹配的Element具有文本內容,而不僅僅是空白。 |
| String | html() | 獲取所有匹配元素的組合內部HTML。 |
| Elements | html?(String html) | 設置每個匹配元素的內部HTML。 |
| boolean | is?(String query) | 測試是否有任何匹配的元素與提供的查詢匹配。 |
| Element | last() | 獲取最后匹配的元素。 |
| Elements | next() | 獲取此列表中每個元素的直接下一個元素同級。 |
| Elements | next?(String query) | 獲取此列表中每個元素的直接下一個元素同級,由查詢過濾。 |
| Elements | nextAll() | 獲取此列表中每個元素的以下每個元素同級。 |
| Elements | nextAll?(String query) | 獲取此列表中與查詢匹配的每個元素的以下每個元素同級。 |
| Elements | not?(String query) | 從此列表中刪除與Selector查詢匹配的元素。 |
| String | outerHtml() | 獲取所有匹配元素的組合外部HTML。 |
| Elements | parents() | 獲取匹配元素的所有父元素和祖先元素。 |
| Elements | prepend?(String html) | 將提供的HTML添加到每個匹配元素的內部HTML的開頭。 |
| Elements | prev() | 獲取此列表中每個元素的直接上一個元素同級。 |
| Elements | prev?(String query) | 獲取此列表中每個元素的直接前一個元素同級,由查詢過濾。 |
| Elements | prevAll() | 獲取此列表中每個元素的每個先前的元素同級。 |
| Elements | prevAll?(String query) | 獲取此列表中與查詢匹配的每個元素的每個先前的元素同級。 |
| Elements | remove() | 從DOM中刪除每個匹配的元素。 |
| Elements | removeAttr?(String attributeKey) | 從每個匹配的元素中刪除一個屬性。 |
| Elements | removeClass?(String className) | 從每個匹配元素的class屬性中刪除類名稱(如果存在)。 |
| Elements | select?(String query) | 在此元素列表中找到匹配的元素。 |
| Elements | tagName?(String tagName) | 更新每個匹配元素的標簽名稱。 |
| String | text() | 獲取所有匹配元素的組合文本。 |
| List<TextNode> | textNodes() | 獲取TextNode作為所選元素的直接子節點的節點。 |
| Elements | toggleClass?(String className) | 在每個匹配元素的class屬性上切換類名稱。 |
| String | toString() | 獲取所有匹配元素的組合外部HTML。 |
| Elements | traverse?(NodeVisitor nodeVisitor) | 對每個選定元素執行深度優先遍歷。 |
| Elements | unwrap() | 從DOM中刪除匹配的元素,并將其子級上移到其父級中。 |
| String | val() | 獲取第一個匹配元素的表單元素的值。 |
| Elements | val?(String value) | 在每個匹配的元素中設置表單元素的值。 |
| Elements | wrap?(String html) | 將提供的HTML環繞每個匹配的元素。 |
public interface Connection 接口
要獲得一個新連接,使用Jsoup.connect(字符串)。連接包含連接。請求和連接。響應對象。請求對象可作為原型請求重用。
所有方法
| Connection | cookie?(String name, String value) | 設置要在請求中發送的cookie。 |
| Connection | cookies?(Map<String,?String> cookies) | 將每個提供的cookie添加到請求中。 |
| Connection.KeyVal | data?(String key) | 獲取此密鑰的數據KeyVal(如果有) |
| Connection | data?(String… keyvals) | 添加許多請求數據參數。 |
| Connection | data?(String key, String value) | 添加請求數據參數。 |
| Connection | data?(String key, String filename, InputStream inputStream) | 添加輸入流作為請求數據參數。 |
| Connection | data?(String key, String filename, InputStream inputStream, String contentType) | 添加輸入流作為請求數據參數。 |
| Connection | data?(Collection<Connection.KeyVal> data) | 將所有提供的數據添加到請求數據參數中 |
| Connection | data?(Map<String,?String> data) | 將所有提供的數據添加到請求數據參數中 |
| Connection.Response | execute() | 執行請求。 |
| Connection | followRedirects?(boolean followRedirects) | 將連接配置為(不)遵循服務器重定向。 |
| Document | get() | 將請求作為GET執行,然后解析結果。 |
| Connection | header?(String name, String value) | 設置請求標頭。 |
| Connection | headers?(Map<String,?String> headers) | 將每個提供的標頭添加到請求中。 |
| Connection | ignoreContentType?(boolean ignoreContentType) | 解析響應時,忽略文檔的Content-Type。 |
| Connection | ignoreHttpErrors?(boolean ignoreHttpErrors) | 將連接配置為在發生HTTP錯誤時不引發異常。 |
| Connection | maxBodySize?(int bytes) | 設置在關閉連接之前,將要從(未壓縮的)連接讀入主體的最大字節數,并截斷輸入(即 |
| Connection | method?(Connection.Method method) | 將請求方法設置為使用GET或POST。 |
| Connection | parser?(Parser parser) | 提供在解析對文檔的響應時要使用的備用解析器。 |
| Document | post() | 將請求作為POST執行,然后解析結果。 |
| Connection | postDataCharset?(String charset) | 設置x-www-form-urlencoded帖子數據的默認帖子數據字符集 |
| Connection | proxy?(String host, int port) | 設置用于此請求的HTTP代理。 |
| Connection | proxy?(Proxy proxy) | 設置要用于此請求的代理。 |
| Connection | referrer?(String referrer) | 設置請求引薦來源網址(又稱為“引薦來源”)標頭。 |
| Connection.Request | request() | 獲取與此連接關聯的請求對象 |
| Connection | request?(Connection.Request request) | 設置連接的請求 |
| Connection | requestBody?(String body) | 設置POST(或PUT)請求正文。 |
| Connection.Response | response() | 請求執行后,獲取響應 |
| Connection | response?(Connection.Response response) | 設置連接的響應 |
| Connection | sslSocketFactory?(SSLSocketFactory sslSocketFactory) | 設置自定義SSL套接字工廠 |
| Connection | timeout?(int millis) | 設置總的請求超時時間。 |
| Connection | url?(String url) | 將請求URL設置為獲取。 |
| Connection | url?(URL url) | 將請求URL設置為獲取。 |
| Connection | userAgent?(String userAgent) | 設置請求用戶代理標頭。 |
總結
以上是生活随笔為你收集整理的jsoup 的基本使用以及API内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Raptor-回文字符串判断
- 下一篇: 拨号上网变成WiFi热点