使用dom方式遍歷文檔
獲取元素
根據id查詢元素getElementById根據標簽獲取元素getElementsByTag根據class獲取元素getElementsByClass根據屬性獲取元素getElementsByAttribute
???????
元素中獲取數據
從元素中獲取id從元素中獲取className從元素中獲取屬性的值attr從元素中獲取所有屬性attributes從元素中獲取文本內容text
@Testpublic void testData() throws Exception {//解析文件,獲取DocumentDocument doc = Jsoup.parse(new File("E:\\ideaCode\\dym-crawler-first\\test.html"), "utf8");//根據id獲取元素Element element = doc.getElementById("test");String str = "";//元素中獲取數據//1. 從元素中獲取idstr = element.id();//2. 從元素中獲取classNamestr = element.className();//Set<String> classSet = element.classNames();//for (String s : classSet ) {// System.out.println(s);//}//3. 從元素中獲取屬性的值attr//str = element.attr("id");str = element.attr("class");//4. 從元素中獲取所有屬性attributesAttributes attributes = element.attributes();System.out.println(attributes.toString());//5. 從元素中獲取文本內容textstr = element.text();//打印獲取到的內容System.out.println("獲取到的數據是:" + str);}
?
總結
以上是生活随笔為你收集整理的使用dom方式遍历文档||获取元素||从元素中获取数据的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。