我现在做一个项目,我现在可以做到利用URL拿到HTML的源码变成String,然后做分析。
但我取得其中的一部分内容时,里面含有很多HTML标签,有没有什么办法可以把标签删除~鉴于HTML标签有可能很多,所以基本不用replace替换,我记得有个朋友说可以用一个开源项目做到这点,但我不知道是那个开源包,有知道的说句,或者有什么好的解决方法也麻烦告诉一声thx~!

解决方案 »

  1.   

    找到了,回帖给分结贴啦http://jerichohtml.sourceforge.net   jar包http://jerichohtml.sourceforge.net/samples/console/src/RenderToText.java我自己测试了一下:import java.net.URL;
    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);
      }
    }
      

  2.   

    之前我写的网络蜘蛛,是通过xml的一个类似正则匹配的技术实现抓取内容的。
    好像是java的Transformer吧因为要分类标题 内容 时间  连接等等
      

  3.   

     /**
      * 功能:去掉所有的<*>标记,去除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;
     }
      

  4.   

    lz尝试一下HTMLParser
    http://htmlparser.sourceforge.net/