zoukankan      html  css  js  c++  java
  • jsoup工具类

    public class HtmlUtils {

    // 只有纯文本可以通过
    public static String getText(String html) {
    if (html == null)
    return null;
    return Jsoup.clean(html, Whitelist.none()).replace(" ", "");
    }

    // 以下标签可以通过
    // b, em, i, strong, u. 纯文本
    public static String getSimpleHtml(String html) {
    if (html == null)
    return null;
    return Jsoup.clean(html, Whitelist.simpleText());
    }

    // 以下标签可以通过
    //a, b, blockquote, br, cite, code, dd, dl, dt, em, i, li, ol, p, pre, q, small, strike, strong, sub, sup, u, ul
    public static String getBasicHtml(String html) {
    if (html == null)
    return null;
    return Jsoup.clean(html, Whitelist.basic());
    }

    //在basic基础上 增加图片通过
    public static String getBasicHtmlandimage(String html) {
    if (html == null)
    return null;
    return Jsoup.clean(html, Whitelist.basicWithImages());
    }
    // 以下标签可以通过
    //a, b, blockquote, br, caption, cite, code, col, colgroup, dd, dl, dt, em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, u, ul
    public static String getFullHtml(String html) {
    if (html == null)
    return null;
    return Jsoup.clean(html, Whitelist.relaxed());
    }

    //只允许指定的html标签
    public static String clearTags(String html, String ...tags) {
    Whitelist wl = new Whitelist();
    return Jsoup.clean(html, wl.addTags(tags));
    }

    // 对关键字加上颜色
    public static String markKeywods (String keywords, String target) {
    if (StringKit.notBlank(keywords)) {
    String[] arr = keywords.split(" ");
    for (String s : arr) {
    if (StringKit.notBlank(s)) {
    String temp = "<span class="highlight">" + s + "</span>";
    if(temp!=null)
    target = target.replaceAll(s, temp);
    }
    }
    }
    return target;
    }

    // 获取文章中的img url
    public static String getImgSrc(String html) {
    if (html == null)
    return null;
    Document doc = Jsoup.parseBodyFragment(html);
    Element image = doc.select("img").first();
    return image == null ? null : image.attr("src");
    }

    ————————————————
    版权声明:本文为CSDN博主「骑猪下不来的猴」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_42529699/article/details/90402489

  • 相关阅读:
    如何解决移动端滚动穿透问题
    如何在mac中通过命令行使用sublime
    正向代理和反向代理
    UTF8、UTF16、UTF16-LE、UTF16-BE、UTF32都是些什么?
    依赖的版本
    如何移除inline-block元素之间的空白
    如何用JavaScript重定向到另一个网页?
    [读书笔记] 高性能网站建设指南
    java使用jconsole查看java程序运行(jmx原理)
    oracle相关知识点
  • 原文地址:https://www.cnblogs.com/java-llp/p/11889085.html
Copyright © 2011-2022 走看看