Jsoup(一)Jsoup详解(官方)

随笔 0 873
小小草
小小草 2021年12月17日 23:07 发表

1.1、简介

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

1.2、Jsoup的主要功能

    1)从一个URL,文件或字符串中解析HTML
    2)使用DOM或CSS选择器来查找、取出数据
    3)可操作HTML元素、属性、文本
    注意:jsoup是基于MIT协议发布的,可放心使用于商业项目。

1.3、jsoup 的主要类层次结构

  

二、入门

2.1、解析和遍历一个HTML文档

  如何解析一个HTML文档:

  1. String html = "<html><head><title>First parse</title></head>"
  2. + "<body><p>Parsed HTML into a doc.</p></body></html>";
  3. Document doc = Jsoup.parse(html);
  其解析器能够尽最大可能从你提供的HTML文档来创见一个干净的解析结果,无论HTML的格式是否完整。比如它可以处理:
    1)没有关闭的标签

  1. <p>Lorem <p>Ipsum parses to <p>Lorem</p> <p>Ipsum</p>
    2)隐式标签

  1. 它可以自动将 <td>Table data</td>包装成<table><tr><td>?
    3)创建可靠的文档结构

  1. html标签包含head 和 body,在head只出现恰当的元素

2.2、一个文档的对象模型

        1)文档由多个Elements和TextNodes组成
        2)其继承结构如下:Document继承Element继承Node. TextNode继承 Node.
        3)一个Element包含一个子节点集合,并拥有一个父Element。他们还提供了一个唯一的子元素过滤列表。

三、输入

3.1、解析一个HTML字符串

  1)存在问题
    来自用户输入,一个文件或一个网站的HTML字符串,你可能需要对它进行解析并取其内容,或校验其格式是否完整,
    或想修改它。怎么办? jsoup能够帮你轻松解决这些问题
  2)解决方法
          使用静态Jsoup.parse(String html) 方法或 Jsoup.parse(String html, String baseUri)

  1. String html = "<html><head><title>First parse</title></head>"
  2. + "<body><p>Parsed HTML into a doc.</p></body></html>";
  3. Document doc = Jsoup.parse(html);
    3)描述
            A:
                parse(String html, String baseUri) 这方法能够将输入的HTML解析为一个新的文档 (Document),参数 baseUri 是用来将相对 URL 转成绝对URL,
      并指定从哪个网站获取文档。如这个方法不适用,你可以使用 parse(String html) 方法来解析成HTML字符串如上面的示例。        
            B:
                只要解析的不是空字符串,就能返回一个结构合理的文档,其中包含(至少) 一个head和一个body元素。
            C:
                一旦拥有了一个Document,你就可以使用Document中适当的方法或它父类 Element和Node中的方法来取得相关数据。

3.2、解析一个body片断

  1)存在问题
          假如你有一个HTML片断 (比如. 一个 div 包含一对 p 标签; 一个不完整的HTML文档) 想对它进行解析。这个HTML片断可以是用户提交的一条评论
    或在一个CMS页面中编辑body部分。
  2)办法
       使用Jsoup.parseBodyFragment(String html)方法。

  1. String html = "<div><p>Lorem ipsum.</p>";
  2. Document doc = Jsoup.parseBodyFragment(html);
  3. Element body = doc.body();
  3)说明
        A:
            parseBodyFragment 方法创建一个空壳的文档,并插入解析过的HTML到body元素中。假如你使用正常的 Jsoup.parse(String html) 方法,
   通常你也可以得到相同的结果,但是明确将用户输入作为 body片段处理,以确保用户所提供的任何糟糕的HTML都将被解析成body元素。 
        B:
            Document.body() 方法能够取得文档body元素的所有子元素,与 doc.getElementsByTag("body")相同。

3.3、从一个URL加载一个Document

  1)存在问题
    你需要从一个网站获取和解析一个HTML文档,并查找其中的相关数据。
  2)解决方法
    使用 Jsoup.connect(String url)方法:

  1. Document doc = Jsoup.connect("http://example.com/").get();
  2. String title = doc.title();
    3)说明
        connect(String url) 方法创建一个新的 Connection, 和 get() 取得和解析一个HTML文件。如果从该URL获取HTML时发生错误,便会抛出 IOException,应适当处理。
        Connection 接口还提供一个方法链来解决特殊请求,具体如下:

  1. Document doc = Jsoup.connect("http://example.com";)
  2. .data("query", "Java")
  3. .userAgent("Mozilla")
  4. .cookie("auth", "token")
  5. .timeout(3000)
  6. .post();
        这个方法只支持Web URLs (http和https 协议); 假如你需要从一个文件加载,可以使用 parse(File in, String charsetName) 代替。

3.4、从一个文件加载文档

  1)存在问题
    在本机硬盘上有一个HTML文件,需要对它进行解析从中抽取数据或进行修改。
  2)方法
    可以使用静态 Jsoup.parse(File in, String charsetName, String baseUri) 方法:

  1. File input = new File("/tmp/input.html");
  2. Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/";);
    3)说明
        A:
            parse(File in, String charsetName, String baseUri) 这个方法用来加载和解析一个HTML文件。如在加载文件的时候发生错误,将抛出IOException,应作适当处理。
        B:
            baseUri 参数用于解决文件中URLs是相对路径的问题。如果不需要可以传入一个空的字符串。
        C:
            另外还有一个方法parse(File in, String charsetName) ,它使用文件的路径做为 baseUri。 这个方法适用于如果被解析文件位于网站的本地文件系统,
    且相关链接也指向该文件系统。

四、数据抽取

4.1、使用DOM方法来遍历一个文档

  1)存在问题
    你有一个HTML文档要从中提取数据,并了解这个HTML文档的结构。
  2)方法
    将HTML解析成一个Document之后,就可以使用类似于DOM的方法进行操作。
  1. File input = new File("/tmp/input.html");
  2. Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
  3.  
  4. Element content = doc.getElementById("content");
  5. Elements links = content.getElementsByTag("a");
  6. for (Element link : links) {
  7. String linkHref = link.attr("href");
  8. String linkText = link.text();
  9. }

  3)说明

    Elements这个对象提供了一系列类似于DOM的方法来查找元素,抽取并处理其中的数据。具体如下:

    A:查看元素

  1.     getElementById(String id)
  2.     getElementsByTag(String tag)
  3.     getElementsByClass(String className)
  4.     getElementsByAttribute(String key) (and related methods)
  5.     Element siblings: siblingElements(), firstElementSibling(), lastElementSibling(); nextElementSibling(), previousElementSibling()
  6.     Graph: parent(), children(), child(int index)

    B:元素数据

  1.     attr(String key)获取属性attr(String key, String value)设置属性
  2.     attributes()获取所有属性
  3.     id(), className() and classNames()
  4.     text()获取文本内容text(String value) 设置文本内容
  5.     html()获取元素内HTMLhtml(String value)设置元素内的HTML内容
  6.     outerHtml()获取元素外HTML内容
  7.     data()获取数据内容(例如:script和style标签)
  8.     tag() and tagName()

    C:操作HTML和文本

  1.     append(String html), prepend(String html)
  2.     appendText(String text), prependText(String text)
  3.     appendElement(String tagName), prependElement(String tagName)
  4.     html(String value)

4.2、使用选择器语法来查找元素

  1)存在问题

    你想使用类似于CSS或jQuery的语法来查找和操作元素。

  2)方法

    可以使用Element.select(String selector) 和 Elements.select(String selector) 方法实现:

  1. File input = new File("/tmp/input.html");
  2. Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
  3.  
  4. Elements links = doc.select("a[href]"); //带有href属性的a元素
  5. Elements pngs = doc.select("img[src$=.png]");
  6. //扩展名为.png的图片
  7.  
  8. Element masthead = doc.select("div.masthead").first();
  9. //class等于masthead的div标签
  10.  
  11. Elements resultLinks = doc.select("h3.r > a"); //在h3元素之后的a元素

  3)说明

    jsoup elements对象支持类似于CSS (或jquery)的选择器语法,来实现非常强大和灵活的查找功能。.

    这个select 方法在Document, Element,或Elements对象中都可以使用。且是上下文相关的,因此可实现指定元素的过滤,或者链式选择访问。

    Select方法将返回一个Elements集合,并提供一组方法来抽取和处理结果。

    A:Selector选择器概述   

      tagname: 通过标签查找元素,比如:a
      ns|tag: 通过标签在命名空间查找元素,比如:可以用 fb|name 语法来查找 <fb:name> 元素
      #id: 通过ID查找元素,比如:#logo
      .class: 通过class名称查找元素,比如:.masthead
      [attribute]: 利用属性查找元素,比如:[href]
      [^attr]: 利用属性名前缀来查找元素,比如:可以用[^>      [attr=value]: 利用属性值来查找元素,比如:[width=500]
      [attr^=value], [attr$=value], [attr*=value]: 利用匹配属性值开头、结尾或包含属性值来查找元素,比如:[href*=/path/]
      [attr~=regex]: 利用属性值匹配正则表达式来查找元素,比如: img[src~=(?i)\.(png|jpe?g)]
      *: 这个符号将匹配所有元素

    B:Selector选择器组合使用

      el#id: 元素+ID,比如: div#logo
      el.class: 元素+class,比如: div.masthead
      el[attr]: 元素+class,比如: a[href]
      任意组合,比如:a[href].highlight
      ancestor child: 查找某个元素下子元素,比如:可以用.body p 查找在"body"元素下的所有 p元素
      parent > child: 查找某个父元素下的直接子元素,比如:可以用div.content > p 查找 p 元素,也可以用body > * 查找body标签下所有直接子元素
      siblingA + siblingB: 查找在A元素之前第一个同级元素B,比如:div.head + div
      siblingA ~ siblingX: 查找A元素之前的同级X元素,比如:h1 ~ p
      el, el, el:多个选择器组合,查找匹配任一选择器的唯一元素,例如:div.masthead, div.logo

    C:伪选择器selectors

      :lt(n): 查找哪些元素的同级索引值(它的位置在DOM树中是相对于它的父节点)小于n,比如:td:lt(3) 表示小于三列的元素
      :gt(n):查找哪些元素的同级索引值大于n,比如: div p:gt(2)表示哪些div中有包含2个以上的p元素
      :eq(n): 查找哪些元素的同级索引值与n相等,比如:form input:eq(1)表示包含一个input标签的Form元素
      :has(seletor): 查找匹配选择器包含元素的元素,比如:div:has(p)表示哪些div包含了p元素
      :not(selector): 查找与选择器不匹配的元素,比如: div:not(.logo) 表示不包含 元素的所有 div 列表
      :contains(text): 查找包含给定文本的元素,搜索不区分大不写,比如: p:contains(jsoup)
      :containsOwn(text): 查找直接包含给定文本的元素
      :matches(regex): 查找哪些元素的文本匹配指定的正则表达式,比如:div:matches((?i)login)
      :matchesOwn(regex): 查找自身包含文本匹配指定正则表达式的元素
      注意:上述伪选择器索引是从0开始的,也就是

4.3、从元素抽取属性,本文和HTML

  1)存在问题

    在解析获得一个Document实例对象,并查找到一些元素之后,你希望取得在这些元素中的数据。

  2)方法  

    要取得一个属性的值,可以使用Node.attr(String key) 方法
    对于一个元素中的文本,可以使用Element.text()方法
    对于要取得元素或属性中的HTML内容,可以使用Element.html(), 或 Node.outerHtml()方法

  1.   String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
  2.   Document doc = Jsoup.parse(html);//解析HTML字符串返回一个Document实现
  3.   Element link = doc.select("a").first();//查找第一个a元素
  4.  
  5.   String text = doc.body().text(); // "An example link"//取得字符串中的文本
  6.   String linkHref = link.attr("href"); // "http://example.com/"//取得链接地址
  7.   String linkText = link.text(); // "example""//取得链接地址中的文本
  8.  
  9.   String linkOuterH = link.outerHtml();
  10.    // "<a href="http://example.com"><b>example</b></a>"
  11.   String linkInnerH = link.html(); // "<b>example</b>"//取得链接内的html内容

  3)说明  

    上述方法是元素数据访问的核心办法。此外还其它一些方法可以使用:

1
2
3
Element.id()
Element.tagName()
Element.className() and Element.hasClass(String className)

    这些访问器方法都有相应的setter方法来更改数据.

4.4、处理URLs

  1)存在问题

    你有一个包含相对URLs路径的HTML文档,需要将这些相对路径转换成绝对路径的URLs。

  2)方法    

    在你解析文档时确保有指定base URI,然后
    使用 abs: 属性前缀来取得包含base URI的绝对路径。代码如下:

  1.   Document doc = Jsoup.connect("http://www.open-open.com").get();
  2.  
  3.   Element link = doc.select("a").first();
  4.   String relHref = link.attr("href"); // == "/"
  5.   String absHref = link.attr("abs:href"); // "http://www.open-open.com/"

  3)说明

    在HTML元素中,URLs经常写成相对于文档位置的相对路径: <a href="/download">...</a>.

    当你使用 Node.attr(String key) 方法来取得a元素的href属性时,它将直接返回在HTML源码中指定定的值。

    假如你需要取得一个绝对路径,需要在属性名前加 abs: 前缀。这样就可以返回包含根路径的URL地址attr("abs:href")

    因此,在解析HTML文档时,定义base URI非常重要。

    如果你不想使用abs: 前缀,还有一个方法能够实现同样的功能 Node.absUrl(String key)。

4.5、实例程序:获取所有连链接

  1)说明

    这个示例程序将展示如何从一个URL获得一个页面。然后提取页面中的所有链接、图片和其它辅助内容。并检查URLs和文本信息。

  2)运行下面程序需要执行一个URLs作为参数

  1. package org.jsoup.examples;
  2.  
  3. import org.jsoup.Jsoup;
  4. import org.jsoup.helper.Validate;
  5. import org.jsoup.nodes.Document;
  6. import org.jsoup.nodes.Element;
  7. import org.jsoup.select.Elements;
  8.  
  9. import java.io.IOException;
  10.  
  11. /**
  12. * Example program to list links from a URL.
  13. */
  14. public class ListLinks {
  15. public static void main(String[] args) throws IOException {
  16. Validate.isTrue(args.length == 1, "usage: supply url to fetch");
  17. String url = args[0];
  18. print("Fetching %s...", url);
  19.  
  20. Document doc = Jsoup.connect(url).get();
  21. Elements links = doc.select("a[href]");
  22. Elements media = doc.select("[src]");
  23. Elements imports = doc.select("link[href]");
  24.  
  25. print("\nMedia: (%d)", media.size());
  26. for (Element src : media) {
  27. if (src.tagName().equals("img"))
  28. print(" * %s: <%s> %sx%s (%s)",
  29. src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
  30. trim(src.attr("alt"), 20));
  31. else
  32. print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
  33. }
  34.  
  35. print("\nImports: (%d)", imports.size());
  36. for (Element link : imports) {
  37. print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));
  38. }
  39.  
  40. print("\nLinks: (%d)", links.size());
  41. for (Element link : links) {
  42. print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
  43. }
  44. }
  45.  
  46. private static void print(String msg, Object... args) {
  47. System.out.println(String.format(msg, args));
  48. }
  49.  
  50. private static String trim(String s, int width) {
  51. if (s.length() > width)
  52. return s.substring(0, width-1) + ".";
  53. else
  54. return s;
  55. }
  56. }

  3)查看结果

  1. Fetching http://news.ycombinator.com/...
  2.  
  3. Media: (38)
  4. * img: <http://ycombinator.com/images/y18.gif> 18x18 ()
  5. * img: <http://ycombinator.com/images/s.gif> 10x1 ()
  6. * img: <http://ycombinator.com/images/grayarrow.gif> x ()
  7. * img: <http://ycombinator.com/images/s.gif> 0x10 ()
  8. * script: <http://www.co2stats.com/propres.php?s=1138>
  9. * img: <http://ycombinator.com/images/s.gif> 15x1 ()
  10. * img: <http://ycombinator.com/images/hnsearch.png> x ()
  11. * img: <http://ycombinator.com/images/s.gif> 25x1 ()
  12. * img: <http://mixpanel.com/site_media/images/mixpanel_partner_logo_borderless.gif> x (Analytics by Mixpan.)
  13.  
  14. Imports: (2)
  15. * link <http://ycombinator.com/news.css> (stylesheet)
  16. * link <http://ycombinator.com/favicon.ico> (shortcut icon)
  17.  
  18. Links: (141)
  19. * a: <http://ycombinator.com> ()
  20. * a: <http://news.ycombinator.com/news> (Hacker News)
  21. * a: <http://news.ycombinator.com/newest> (new)
  22. * a: <http://news.ycombinator.com/newcomments> (comments)
  23. * a: <http://news.ycombinator.com/leaders> (leaders)
  24. * a: <http://news.ycombinator.com/jobs> (jobs)
  25. * a: <http://news.ycombinator.com/submit> (submit)
  26. * a: <http://news.ycombinator.com/x?fnid=JKhQjfU7gW> (login)
  27. * a: <http://news.ycombinator.com/vote?for=1094578&dir=up&whence=%6e%65%77%73> ()
  28. * a: <http://www.readwriteweb.com/archives/facebook_gets_faster_debuts_homegrown_php_compiler.php?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+readwriteweb+%28ReadWriteWeb%29&utm_content=Twitter> (Facebook speeds up PHP)
  29. * a: <http://news.ycombinator.com/user?id=mcxx> (mcxx)
  30. * a: <http://news.ycombinator.com/item?id=1094578> (9 comments)
  31. * a: <http://news.ycombinator.com/vote?for=1094649&dir=up&whence=%6e%65%77%73> ()
  32. * a: <http://groups.google.com/group/django-developers/msg/a65fbbc8effcd914> ("Tough. Django produces XHTML.")
  33. * a: <http://news.ycombinator.com/user?id=andybak> (andybak)
  34. * a: <http://news.ycombinator.com/item?id=1094649> (3 comments)
  35. * a: <http://news.ycombinator.com/vote?for=1093927&dir=up&whence=%6e%65%77%73> ()
  36. * a: <http://news.ycombinator.com/x?fnid=p2sdPLE7Ce> (More)
  37. * a: <http://news.ycombinator.com/lists> (Lists)
  38. * a: <http://news.ycombinator.com/rss> (RSS)
  39. * a: <http://ycombinator.com/bookmarklet.html> (Bookmarklet)
  40. * a: <http://ycombinator.com/newsguidelines.html> (Guidelines)
  41. * a: <http://ycombinator.com/newsfaq.html> (FAQ)
  42. * a: <http://ycombinator.com/newsnews.html> (News News)
  43. * a: <http://news.ycombinator.com/item?id=363> (Feature Requests)
  44. * a: <http://ycombinator.com> (Y Combinator)
  45. * a: <http://ycombinator.com/w2010.html> (Apply)
  46. * a: <http://ycombinator.com/lib.html> (Library)
  47. * a: <http://www.webmynd.com/html/hackernews.html> ()
  48. * a: <http://mixpanel.com/?from=yc> ()

五、数据修改

5.1、设置属性值

  1)存在问题   

    在你解析一个Document之后可能想修改其中的某些属性值,然后再保存到磁盘或都输出到前台页面。

  2)方法 

    可以使用属性设置方法 Element.attr(String key, String value), 和 Elements.attr(String key, String value).

    假如你需要修改一个元素的 class 属性,可以使用 Element.addClass(String className) 和 Element.removeClass(String className) 方法。

    Elements 提供了批量操作元素属性和class的方法,比如:要为div中的每一个a元素都添加一个 rel="nofollow" 可以使用如下方法:

    doc.select("div.comments a").attr("rel", "nofollow");

  3)说明

    与Element中的其它方法一样,attr 方法也是返回当 Element (或在使用选择器是返回 Elements 集合)。

    这样能够很方便使用方法连用的书写方式。比如:

    doc.select("div.masthead").attr("title", "jsoup").addClass("round-box");

5.2、设置一个元素的HTML内容

  1)存在问题

    你需要一个元素中的HTML的内容

  2)方法

    可以使用Element中的HTML设置方法具体如下:

  1. Element div = doc.select("div").first(); // <div></div>
  2. div.html("<p>lorem ipsum</p>"); // <div><p>lorem ipsum</p></div>
  3. div.prepend("<p>First</p>");//在div前添加html内容
  4. div.append("<p>Last</p>");//在div之后添加html内容
  5. // 添完后的结果: <div><p>First</p><p>lorem ipsum</p><p>Last</p></div>
  6.  
  7. Element span = doc.select("span").first(); // <span>One</span>
  8. span.wrap("<li><a href='http://example.com/'></a></li>");
  9. // 添完后的结果: <li><a href="http://example.com"><span>One</span></a></li>

  3)说明

    Element.html(String html) 这个方法将先清除元素中的HTML内容,然后用传入的HTML代替。
    Element.prepend(String first) 和 Element.append(String last) 方法用于在分别在元素内部HTML的前面和后面添加HTML内容
    Element.wrap(String around) 对元素包裹一个外部HTML内容。

点赞 0 收藏(0)    分享
相关标签: Jsoup
问题没解决?让chatGPT帮你作答 智能助手
0 个评论
  • 消灭零评论