Java实现从Html文本中提取纯文本

1、应用场景:从一份html文件中或从String(是html内容)中提取纯文本,去掉网页标签;

2、代码一:replaceAll搞定

[java]  view plain  copy
  1. //从html中提取纯文本  
  2. public static String StripHT(String strHtml) {  
  3.      String txtcontent = strHtml.replaceAll("</?[^>]+>"""); //剔出<html>的标签    
  4.         txtcontent = txtcontent.replaceAll("<a>\\s*|\t|\r|\n</a>""");//去除字符串中的空格,回车,换行符,制表符    
  5.         return txtcontent;  
  6.    }  

3、代码二:正则表达式搞定

[java]  view plain  copy
  1. //从html中提取纯文本  
  2.     public static String Html2Text(String inputString) {  
  3.         String htmlStr = inputString; // 含html标签的字符串  
  4.         String textStr = "";  
  5.         java.util.regex.Pattern p_script;  
  6.         java.util.regex.Matcher m_script;  
  7.         java.util.regex.Pattern p_style;  
  8.         java.util.regex.Matcher m_style;  
  9.         java.util.regex.Pattern p_html;  
  10.         java.util.regex.Matcher m_html;  
  11.         try {  
  12.             String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"// 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>  
  13.             String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"// 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>  
  14.             String regEx_html = "<[^>]+>"// 定义HTML标签的正则表达式  
  15.             p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);  
  16.             m_script = p_script.matcher(htmlStr);  
  17.             htmlStr = m_script.replaceAll(""); // 过滤script标签  
  18.             p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);  
  19.             m_style = p_style.matcher(htmlStr);  
  20.             htmlStr = m_style.replaceAll(""); // 过滤style标签  
  21.             p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);  
  22.             m_html = p_html.matcher(htmlStr);  
  23.             htmlStr = m_html.replaceAll(""); // 过滤html标签  
  24.             textStr = htmlStr;  
  25.         } catch (Exception e) {System.err.println("Html2Text: " + e.getMessage()); }  
  26.         //剔除空格行  
  27.         textStr=textStr.replaceAll("[ ]+"" ");  
  28.         textStr=textStr.replaceAll("(?m)^\\s*$(\\n|\\r\\n)""");  
  29.         return textStr;// 返回文本字符串  
  30.     }  

3、代码三:HTMLEditorKit.ParserCallback搞定,Java自带的类

    

[java]  view plain  copy
  1. package com.util;  
  2.   
  3. import java.io.*;  
  4. import javax.swing.text.html.*;  
  5. import javax.swing.text.html.parser.*;  
  6.   
  7. public class Html2Text extends HTMLEditorKit.ParserCallback {  
  8.      StringBuffer s;  
  9.   
  10.      public Html2Text() {}  
  11.   
  12.      public void parse(Reader in) throws IOException {  
  13.        s = new StringBuffer();  
  14.        ParserDelegator delegator = new ParserDelegator();  
  15.        // the third parameter is TRUE to ignore charset directive  
  16.        delegator.parse(in, this, Boolean.TRUE);  
  17.      }  
  18.   
  19.      public void handleText(char[] text, int pos) {  
  20.        s.append(text);  
  21.      }  
  22.   
  23.      public String getText() {  
  24.        return s.toString();  
  25.      }  
  26.   
  27.      public static void main (String[] args) {  
  28.        try {  
  29.          // the HTML to convert  
  30.          //Reader in=new StringReader("string");      
  31.          FileReader in = new FileReader("java-new.html");  
  32.          Html2Text parser = new Html2Text();  
  33.          parser.parse(in);  
  34.          in.close();  
  35.          System.out.println(parser.getText());  
  36.        }  
  37.        catch (Exception e) {  
  38.          e.printStackTrace();  
  39.        }  
  40.      }  
  41. }  

猜你喜欢

转载自blog.csdn.net/a13662080711/article/details/80223956