我现在做一个项目,我现在可以做到利用URL拿到HTML的源码变成String,然后做分析。
但我取得其中的一部分内容时,里面含有很多HTML标签,有没有什么办法可以把标签删除~鉴于HTML标签有可能很多,所以基本不用replace替换,我记得有个朋友说可以用一个开源项目做到这点,但我不知道是那个开源包,有知道的说句,或者有什么好的解决方法也麻烦告诉一声thx~!
但我取得其中的一部分内容时,里面含有很多HTML标签,有没有什么办法可以把标签删除~鉴于HTML标签有可能很多,所以基本不用replace替换,我记得有个朋友说可以用一个开源项目做到这点,但我不知道是那个开源包,有知道的说句,或者有什么好的解决方法也麻烦告诉一声thx~!
import net.htmlparser.jericho.Source;public class RenderToText {
public static void main(String[] args) throws Exception {
String s="谁也不能在这里天长地久<br><br>终究都是要走的人<br><br>只要开心<br><br>没有什么大不了<br><br>祝福楼主:)<br>";
Source source=new Source(s);
String renderedText=source.getRenderer().toString();
System.out.println("\nSimple rendering of the HTML document:\n");
System.out.println(renderedText);
}
}不过这个对超链接解释有点怪:public class RenderToText {
public static void main(String[] args) throws Exception {
String s="谁也不能在这里天长地久<br><br>终究都是要走的人<br><br>只要开心<br><br>没有什么大不了<br><br>祝福楼主:)<br>" +
"<a href='www.163.com'>fdadaf</a>";
Source source=new Source(s);
String renderedText=source.getRenderer().toString();
System.out.println("\nSimple rendering of the HTML document:\n");
System.out.println(renderedText);
}
}
好像是java的Transformer吧因为要分类标题 内容 时间 连接等等
* 功能:去掉所有的<*>标记,去除html标签
*
* @param content
* @return
*/
public String removeTagFromText(String content) {
Pattern p = null;
Matcher m = null;
String value = null; // 去掉<>标签
p = Pattern.compile("(<[^>]*>)");
m = p.matcher(content);
String temp = content;
while (m.find()) {
value = m.group(0);
temp = temp.replace(value, "");
} // 去掉换行或回车符号
p = Pattern.compile("(\r+|\n+)");
m = p.matcher(temp);
while (m.find()) {
value = m.group(0);
temp = temp.replace(value, " ");
// System.out.println("....." + value);
} return temp;
}
http://htmlparser.sourceforge.net/