转载:Jsoup常用方法功能介绍(html解析器)

jsoup 的作用:是一款 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操作方法来取出和操作数据

1.jsoup的主要功能如下:

  • 从一个URL,文件或字符串中解析HTML; 
  • 使用DOM或CSS选择器来查找、取出数据;
  • 可操作HTML元素、属性、文本;

jsoup解析

1.Jsoup提供一系列的静态解析方法生成Document对象

  • static Document parse(File in, String charsetName)
  • static Document parse(File in, String charsetName, String baseUri)
  • static Document parse(InputStream in, String charsetName, String baseUri)
  • static Document parse(String html)
  • static Document parse(String html, String baseUri)   
  • static Document parse(URL url, int timeoutMillis)
  • static Document parseBodyFragment(String bodyHtml)
  • static Document parseBodyFragment(String bodyHtml, String baseUri) 

   其中baseUri表示检索到的相对URL是相对于baseUriURL的 

2.其中charsetName表示字符集

  • Connection connect(String url) 根据给定的url(必须是http或https)来创建连接
  • Connection 提供一些方法来抓去网页内容
  • Connection cookie(String name, String value) 发送请求时放置cookie 
  • Connection data(Map<String,String> data) 传递请求参数 
  • Connection data(String... keyvals) 传递请求参数
  • Document get() 以get方式发送请求并对返回结果进行解析
  • Document post()以post方式发送请求并对返回结果进行解析 
  • Connection userAgent(String userAgent) 
  • Connection header(String name, String value) 添加请求头
  • Connection referrer(String referrer) 设置请求来源

3.jsoup提供类似JS获取html元素

  • getElementById(String id) 用id获得元素
  • getElementsByTag(String tag) 用标签获得元素
  • getElementsByClass(String className) 用class获得元素
  • getElementsByAttribute(String key)  用属性获得元素
  • 同时还提供下面的方法提供获取兄弟节点:siblingElements(), firstElementSibling(), lastElementSibling();nextElementSibling(), previousElementSibling()

4.获得与设置元素的数据

  • attr(String key)  获得元素的数据 attr(String key, String value) 设置元素数据 
  • attributes() 获得所以属性
  • id(), className()  classNames() 获得id class得值
  • text()获得文本值
  • text(String value) 设置文本值
  • html() 获取html 
  • html(String value)设置html
  • outerHtml() 获得内部html
  • data()获得数据内容
  • tag()  获得tag 和 tagName() 获得tagname 

5.操作html元素:

  • append(String html), prepend(String html)
  • appendText(String text), prependText(String text)
  • appendElement(String tagName), prependElement(String tagName)
  • html(String value)

6.jsoup还提供了类似于JQuery方式的选择器
  6.1.采用选择器来检索数据

  • tagname 使用标签名来定位,例如 a 
  • ns|tag     使用命名空间的标签定位,例如 fb:name 来查找 <fb:name> 元素 
  • #id     使用元素 id 定位,例如 #logo 
  • .class     使用元素的 class 属性定位,例如 .head 
  • *     定位所有元素 
  • [attribute] 使用元素的属性进行定位,例如 [href] 表示检索具有 href 属性的所有元素 
  • [^attr] 使用元素的属性名前缀进行定位,例如 [^data-] 用来查找 HTML5 的 dataset 属性 
  • [attr=value]使用属性值进行定位,例如 [width=500] 定位所有 width 属性值为 500 的元素 
  • [attr^=value],[attr$=value],[attr*=value] 这三个语法分别代表,属性以 value 开头、结尾以及包含 
  • [attr~=regex]使用正则表达式进行属性值的过滤,例如 img[src~=(?i)\.(png|jpe?g)] 
  • 以上是最基本的选择器语法,这些语法也可以组合起来使用

7.组合用法

  • el#id      定位id值某个元素,例如 a#logo -> <a id=logo href= … > 
  • el.class 定位 class 为指定值的元素,例如 div.head -> <div class="head">xxxx</div> 
  • el[attr] 定位所有定义了某属性的元素,例如 a[href] 
  • 以上三个任意组合     例如 a[href]#logo 、a[name].outerlink 
  • 除了一些基本的语法以及这些语法进行组合外,jsoup 还支持使用表达式进行元素过滤选择
  • :lt(n)     例如 td:lt(3) 表示小于三列 
  • :gt(n)     div p:gt(2) 表示 div 中包含 2 个以上的 p 
  • :eq(n)     form input:eq(1) 表示只包含一个 input 的表单 
  • :has(seletor)     div:has(p) 表示包含了 p 元素的 div 
  • :not(selector)     div:not(.logo) 表示不包含 class="logo" 元素的所有 div 列表 
  • :contains(text)     包含某文本的元素,不区分大小写,例如 p:contains(oschina) 
  • :containsOwn(text)     文本信息完全等于指定条件的过滤 
  • :matches(regex)     使用正则表达式进行文本过滤:div:matches((?i)login) 
  • :matchesOwn(regex)     使用正则表达式找到自身的文本 

8.其他用法:

  • //url网址作为输入源
  • Document doc = Jsoup.connect("http://www.example.com").timeout(60000).get();
  • //File文件作为输入源
  • File input = new File("/tmp/input.html");
  • Document doc = Jsoup.parse(input, "UTF-8", "http://www.example.com/");
  • //String作为输入源
  • Document doc = Jsoup.parse(htmlStr);
  • 和java script类似,Jsoup提供了下列的函数
  • getElementById(String id) 通过id获得元素
  • getElementsByTag(String tag) 通过标签获得元素
  • getElementsByClass(String className) 通过class获得元素
  • getElementsByAttribute(String key) 通过属性获得元素

9.同时还提供下面的方法提供获取兄弟节点:

  • siblingElements(), firstElementSibling(), lastElementSibling();nextElementSibling(), previousElementSibling()

10用下面方法获得元素的数据:

  • attr(String key) 获得元素的数据
  • attr(String key, String value) 设置元素数据
  • attributes() 获得所有属性
  • id(), className() classNames() 得到id class的值
  • text()得到文本值
  • text(String value) 设置文本值
  • html() 获取html 
  • html(String value)设置html
  • outerHtml() 获得内部html
  • data()获得数据内容
  • tag() 得到tag 和 tagName() 得到tagname

11.操作html提供了下面方法:

  • append(String html), prepend(String html)
  • appendText(String text), prependText(String text)
  • appendElement(String tagName), prependElement(String tagName)
  • html(String value)

本文来自 Key_MQL 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/key_mql/article/details/55522325?utm_source=copy

posted @ 2018-09-22 10:36  A小小高  阅读(895)  评论(0编辑  收藏  举报