Java實(shí)現(xiàn)從Html文本中提取純文本的方法
1、應(yīng)用場(chǎng)景:從一份html文件中或從String(是html內(nèi)容)中提取純文本,去掉網(wǎng)頁(yè)標(biāo)簽;
2、代碼一:replaceAll搞定
//從html中提取純文本 public static String StripHT(String strHtml) { String txtcontent = strHtml.replaceAll("</?[^>]+>", ""); //剔出<html>的標(biāo)簽 txtcontent = txtcontent.replaceAll("<a>\\s*|\t|\r|\n</a>", "");//去除字符串中的空格,回車,換行符,制表符 return txtcontent; }
3、代碼二:正則表達(dá)式搞定
//從html中提取純文本 public static String Html2Text(String inputString) { String htmlStr = inputString; // 含html標(biāo)簽的字符串 String textStr = ""; java.util.regex.Pattern p_script; java.util.regex.Matcher m_script; java.util.regex.Pattern p_style; java.util.regex.Matcher m_style; java.util.regex.Pattern p_html; java.util.regex.Matcher m_html; try { String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定義script的正則表達(dá)式{或<script[^>]*?>[\\s\\S]*?<\\/script> String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定義style的正則表達(dá)式{或<style[^>]*?>[\\s\\S]*?<\\/style> String regEx_html = "<[^>]+>"; // 定義HTML標(biāo)簽的正則表達(dá)式 p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); // 過(guò)濾script標(biāo)簽 p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); // 過(guò)濾style標(biāo)簽 p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); // 過(guò)濾html標(biāo)簽 textStr = htmlStr; } catch (Exception e) {System.err.println("Html2Text: " + e.getMessage()); } //剔除空格行 textStr=textStr.replaceAll("[ ]+", " "); textStr=textStr.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", ""); return textStr;// 返回文本字符串 }
4、代碼三:HTMLEditorKit.ParserCallback搞定,Java自帶的類
package com.util; import java.io.*; import javax.swing.text.html.*; import javax.swing.text.html.parser.*; public class Html2Text extends HTMLEditorKit.ParserCallback { StringBuffer s; public Html2Text() {} public void parse(Reader in) throws IOException { s = new StringBuffer(); ParserDelegator delegator = new ParserDelegator(); // the third parameter is TRUE to ignore charset directive delegator.parse(in, this, Boolean.TRUE); } public void handleText(char[] text, int pos) { s.append(text); } public String getText() { return s.toString(); } public static void main (String[] args) { try { // the HTML to convert //Reader in=new StringReader("string"); FileReader in = new FileReader("java-new.html"); Html2Text parser = new Html2Text(); parser.parse(in); in.close(); System.out.println(parser.getText()); } catch (Exception e) { e.printStackTrace(); } } }
以上這篇Java實(shí)現(xiàn)從Html文本中提取純文本的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot定時(shí)器實(shí)現(xiàn)定時(shí)同步數(shù)據(jù)的操作步驟
在Java中,@Scheduled注解是用于指定定時(shí)任務(wù)的執(zhí)行規(guī)則的,這篇文章給大家介紹spring boot定時(shí)器實(shí)現(xiàn)定時(shí)同步數(shù)據(jù)的操作步驟,感興趣的朋友一起看看吧2023-12-12利用Java多線程技術(shù)導(dǎo)入數(shù)據(jù)到Elasticsearch的方法步驟
這篇文章主要介紹了利用Java多線程技術(shù)導(dǎo)入數(shù)據(jù)到Elasticsearch的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07springboot?maven?打包插件介紹及注意事項(xiàng)說(shuō)明
這篇文章主要介紹了springboot?maven?打包插件介紹及注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12springboot發(fā)送郵件功能的實(shí)現(xiàn)代碼
發(fā)郵件是一個(gè)很常見(jiàn)的功能,在java中實(shí)現(xiàn)需要依靠JavaMailSender這個(gè)接口,今天通過(guò)本文給大家分享springboot發(fā)送郵件功能的實(shí)現(xiàn)代碼,感興趣的朋友跟隨小編一起看看吧2021-07-07基于Docker的K8s(Kubernetes)集群部署方案
這篇文章主要介紹了基于Docker的K8s(Kubernetes)集群部署方案,文中介紹了安裝k8s的可視化界面的相關(guān)操作,需要的朋友可以參考下2024-01-01Mybatis分頁(yè)插件PageHelper手寫實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Mybatis分頁(yè)插件PageHelper手寫實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08