步骤1:c.html
步骤2:Test.java
步骤3:可运行项目
步骤 1 : c.html
首先准备一个 c.html,用于本知识点的教学。
< html > < body > < div id = "d1" class = "c1 c2" >let us< a href = "how2j.cn" >study java</ a ></ div > </ body > </ html > |
步骤 2 : Test.java
注释都在代码里了,直接看代码即可
package cn.how2j.jsoup; import java.io.File; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; public class Test { public static void main(String[] args) throws Exception { File f = new File( "c.html" ); if (!f.exists()) return ; Document doc = Jsoup.parse(f, "utf-8" ); Element e =doc.getElementById( "d1" ); //获取属性 System.out.println(e.attr( "id" )); //获取所有属性 System.out.println(e.attributes()); //获取id System.out.println(e.id()); //获取类名称 System.out.println(e.className()); //获取所有类名称 System.out.println(e.classNames()); //获取文本 System.out.println(e.text()); //获取html System.out.println(e.html()); //获取外html System.out.println(e.outerHtml()); //获取标签信息 System.out.println(e.tagName()); } } |
步骤 3 : 可运行项目
在下载区(点击进入)有本知识点对应的可运行项目下载 ,实在自己搞不出来,就下载解压出来比较一下。
更多内容,点击了解: https://how2j.cn/k/jsoup/jsoup-text/2152.html