Jsoup类

1、简介html

Jsoup是一款HTML解析器,能够直接解析url地址,也能够解析html文本内容。也可经过DOM、CSS以及相似于jQuery的操做方法来取出和操做数据。其主要功能:安全

一、从url、字符串或者文本中解析出htmlcookie

二、查找、取出数据post

三、操做html元素、属性、文本。测试

Jsoup直接继承Object类,声明为:public class Jsoup extends Object 编码

这是使用Jsoup库的核心的公共的入口。url

2、方法详细code

一、public static Document parse(String html, String baseUri)  将html解析到Document中,这里能为任何html建立一个document文档树。htm

其中的baseUri,html中url常常表示成相对路劲形式,baseUri就是用来指定其根路劲,在解析html中url从相对路劲中转换为绝对路劲时很是重要。对象

二、public static Document parse(String html, String baseUri, Parser parser) 使用指定的解析器对html字符串进行解析。

三、public static Document parse(String html) 将html字符串解析到Document中,这里没有指定baseUri,其依赖于html中<base href>标签。

四、public static Connection connect(String url)   建立一个指定url的连接(Connection)对象,经常使用来获取或解析html页面。

如:Document doc = Jsoup.connect("http://example.com").userAgent("Mozilla").data("name", "jsoup").get();

   Document doc = Jsoup.connect("http://example.com").cookie("auth", "token").post();

五、public static Document parse(File in, String charsetName, String baseUri) throws IOException  解析html文件

charsetName指编码,一般设置为UTF-8比较安全。当文件找不到或者不可读或者编码无效时将会跑IO异常。

六、public static Document parse(File in, String charsetName) throws IOException  解析html文件 文件位置经常使用来做为baseUri。 其余跟上面第5点同样。

七、public static Document parse(InputStream in, String charsetName, String baseUri) throws IOException  读取输入流,而后将其解析为Document对象。

八、public static Document parse(InputStream in, String charsetName, String baseUri, Parser parser) throws IOException  读取输入流,使用指定解析器对其进行解析。

九、public static Document parseBodyFragment(String bodyHtml, String baseUri)  解析只含body部分的html片断。 指定了baseUri

十、public static Document parseBodyFragment(String bodyHtml) 解析只含body部分的html片断。 未指定baseUri

十一、public static Document parse(URL url, int timeoutMillis) throws IOException 将url指定的html解析为document。考虑兼容性经常使用connect(String url)代替。

若是响应返回码不是200或者读取响应流出错将抛出IO异常。

十二、public static String clean(String bodyHtml, String baseUri, Whitelist whitelist)  使用白名单标签和属性对输入的不信任的html进行过滤来获得安全的html。指定了baseUri

1三、public static String clean(String bodyHtml, Whitelist whitelist)   使用白名单标签和属性对输入的不信任的html进行过滤来获得安全的html。未指定baseUri

1四、public static boolean isValid(String bodyHtml, Whitelist whitelist)   测试输入的html是否只包含白名单容许的标签和属性。

相关文章
相关标签/搜索