close
DOM就是Document Object Model
也就是將整個html檔案看成一個tree, 上面有很多節點和內容值
以下就介紹其作法
1. 官網https://jsoup.org/cookbook/extracting-data/dom-navigation
2. 這邊介紹將html的中的每行取出來
在使用的時候, 可以用Elements或Element來裝取內容
用getElementsByTag("Tag名稱")等等之類的方式來取得
String html = "<html><head><title>First parse</title></head>"
+ "<body>"
+ "<p>Line 1.</p>"
+ "<p>Line 2.</p>"
+ "<p>Line 3.</p>"
+ "</body></html>";
Document doc = Jsoup.parse(html);
Elements contents = doc.getElementsByTag("p");
for (Element content : contents) {
System.out.println(content.text());
}
3. 下列是相關的methos
找元素
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)
to get andattr(String key, String value)
to set attributesattributes()
to get all attributesid()
,className()
andclassNames()
text()
to get andtext(String value)
to set the text contenthtml()
to get andhtml(String value)
to set the inner HTML contentouterHtml()
to get the outer HTML valuedata()
to get data content (e.g. ofscript
andstyle
tags)tag()
andtagName()
- 操作html或Text
文章標籤
全站熱搜