1. 程式人生 > >Java實現從Html文字中提取純文字

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. import java.io.*;

  3. import javax.swing.text.html.*;

  4. import javax.swing.text.html.parser.*;

  5. public class Html2Text extends HTMLEditorKit.ParserCallback {

  6. StringBuffer s;

  7. public Html2Text() {}

  8. public void parse(Reader in) throws IOException {

  9. s = new StringBuffer();

  10. ParserDelegator delegator = new ParserDelegator();

  11. // the third parameter is TRUE to ignore charset directive

  12. delegator.parse(in, this, Boolean.TRUE);

  13. }

  14. public void handleText(char[] text, int pos) {

  15. s.append(text);

  16. }

  17. public String getText() {

  18. return s.toString();

  19. }

  20. public static void main (String[] args) {

  21. try {

  22. // the HTML to convert

  23. //Reader in=new StringReader("string");

  24. FileReader in = new FileReader("java-new.html");

  25. Html2Text parser = new Html2Text();

  26. parser.parse(in);

  27. in.close();

  28. System.out.println(parser.getText());

  29. }

  30. catch (Exception e) {

  31. e.printStackTrace();

  32. }

  33. }