步骤1:获取 Document
步骤2:代码演示
步骤 1 : 获取 Document
获取Document对象的方式有多种,常见的就是基于字符串,文件,网页地址。
步骤 2 : 代码演示
其中 a.html 在下载区(点击进入)的可运行项目里

package cn.how2j.jsoup; import java.io.File; import java.net.URL; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class Test { public static void main(String[] args) throws Exception { String html1 = "<html><body><p>Hello HTML</p></body></html>" ; Document doc1 = Jsoup.parse(html1); System.out.println( "基于字符串方式得到的 Document:\r\n" + doc1); File f = new File( "a.html" ); if (f.exists()) { Document doc2 = Jsoup.parse(f, "utf-8" ); System.out.println( "基于文件方式得到的 Document:\r\n" + doc2); } String url = "http://www.baidu.com" ; Document doc3 = Jsoup.parse( new URL(url), 5000 ); //超过5秒就报错 System.out.println( "基于URL方式得到的 Document:\r\n" + doc3); } } |
更多内容,点击了解: https://how2j.cn/k/jsoup/jsoup-doc/2150.html