Java 过滤Html标签示例

最近在公司负责网络爬虫这块,需要过滤文本结果中的Html标签,研究了一下实现了该功能。


示例代码

package com.yulore.ex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;

public class HTMLFilterTest {

	@Test
	public void test01(){
		
		String html = "8:00-17:00                      周六、周日不休 <a href='http://www.123.com'>010-23456789</a>";
		html = filterHTMLTag(html);
		System.out.println(html);
		
	}
	
	@Test
	public void test02(){
		
		StringBuffer htmlStr = new StringBuffer();  
        htmlStr.append("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>")  
               .append("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'><head><title>aaa</title><mce:script type='text/javascript'></mce:script>")  
               .append("<link href='static_files/help.css' mce_href='static_files/help.css' rel='stylesheet' type='text/css' media='all' />")  
               .append("</head><body><ul><li>XXXX</li></ul></body></html>");
        
		String html = filterHtml(htmlStr.toString());
		System.out.println(html);
		
	}
	
	/**
	 * 过滤文本中的所有 html标签,过滤所有的空格并且只保留第一个空格
	 * @param html
	 * @return
	 */
	private String filterHTMLTag(String html) {
		if (html == null || "".equals(html)) {
			return "";
		}
		html = html.replaceAll("<[\\s\\S]*?>", "");	//过滤html标签
		html = html.replaceAll(" ", "");	//过滤  
		html = html.replaceAll("\\s+", " ");	//过滤空格,并保留一个空格
		
		return html;
	}

	/**
	 * 过滤文本中的所有 html标签
	 * @param htmlStr
	 * @return
	 */
	private String filterHtml(String htmlStr) {

		String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[//s//S]*?<///script>
		String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[//s//S]*?<///style>
		String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
		String regEx_html1 = "<[^>]+";
		
		Pattern p_script = Pattern.compile(regEx_script,
				Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll(""); // 过滤script标签

		Pattern p_style = Pattern
				.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll(""); // 过滤style标签

		Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
		Matcher m_html = p_html.matcher(htmlStr);
		htmlStr = m_html.replaceAll(""); // 过滤html标签

		Pattern p_html1 = Pattern
				.compile(regEx_html1, Pattern.CASE_INSENSITIVE);
		Matcher m_html1 = p_html1.matcher(htmlStr);
		htmlStr = m_html1.replaceAll(""); // 过滤html标签

		return htmlStr;
	}
}



OK,大功告成啦!!!




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值