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

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

2、代码一:replaceAll搞定

 

 
  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、代码二:正则表达式搞定

 

 
  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自带的类

    

 
  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. }

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值