020-85548809,29883069

网站设计、网站优化、微信开发

资讯

分享你我感悟

您当前位置>主页 > 资讯 > 网页自动提取标签栏如何设置

网页自动提取标签栏如何设置

     最近发现越来越多的网站喜欢使用自动提取标签栏,如小编常去的糗事百科网站就有一个,截图如下:

网页自动提取标签栏如何设置

 

如上图。大家看右边部分,搜索框下方就是一个自动提取的标签栏。通过点击这些出现频率比较高的关键词,用户可以看到所有出现过此关键词的帖子。结合站内搜索框来说,即成为一个强大的搜索工具。对于网站内容多,更新快,分类多等类型的网站来说,是比搜索框更便利的搜索工具,也将成为网页设计的潮流趋势之一。

虽然网页设计越来越便捷,但是这背后设计师所付出的努力却是艰辛的。我们看糗事百科一个看似很简单的网站,但是如果请专业的网站建设公司来制作的话,成本最低都要好几万。这也能解释为什么我们看起来不起眼的随手的一个工具,说不定都凝聚了大批的设计师长时间的努力结果。

这个自动提取标签的设计也是一样。小编研究了一段时间,搜索了很多资料,才得出比较简单一点的设置方法。实际上这个方法并非对所有的程序都有效,而只是对JAVA程序而言的。在此将此段程序贴上来与大家分享:

  1. import java.io.BufferedReader;  
  2. import java.io.InputStreamReader;  
  3. import java.net.URL;  
  4. import java.util.regex.Matcher;  
  5. import java.util.regex.Pattern;  
  6.   
  7. public class URLTest {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      * @throws URISyntaxException  
  12.      */  
  13.     public static void main(String[] args) throws Exception {  
  14.         URL url = new URL("http://www.ascii-code.com/");  
  15.         InputStreamReader reader = new InputStreamReader(url.openStream());  
  16.         BufferedReader br = new BufferedReader(reader);  
  17.         String s = null;  
  18.         while((s=br.readLine())!=null){  
  19.             s = GetLabel(s);  
  20.             if(s!=null){  
  21.                 System.out.println(s);  
  22.             }  
  23.         }  
  24.         br.close();  
  25.         reader.close();  
  26.     }  
  27.       
  28.     public static String GetContent(String html) {  
  29.         //String html = "
    • 1.hehe
    • 2.hi
    • 3.hei
    ";
      
  30.         String ss = ">[^<]+<";  
  31.         String temp = null;  
  32.         Pattern pa = Pattern.compile(ss);  
  33.         Matcher ma = null;  
  34.         ma = pa.matcher(html);  
  35.         String result = null;  
  36.         while(ma.find()){  
  37.             temp = ma.group();  
  38.             if(temp!=null){  
  39.                 if(temp.startsWith(">")){  
  40.                     temp = temp.substring(1);  
  41.                 }  
  42.                 if(temp.endsWith("<")){  
  43.                     temp = temp.substring(0, temp.length()-1);  
  44.                 }  
  45.                 if(!temp.equalsIgnoreCase("")){  
  46.                     if(result==null){  
  47.                         result = temp;  
  48.                     }  
  49.                     else{  
  50.                         result+="____"+temp;  
  51.                     }  
  52.                 }  
  53.             }  
  54.         }  
  55.         return result;  
  56.     }  
  57.       
  58.     public static String GetLabel(String html) {  
  59.         //String html = "
    • 1.hehe
    • 2.hi
    • 3.hei
    ";
      
  60.         String ss = "<[^>]+>";  
  61.         String temp = null;  
  62.         Pattern pa = Pattern.compile(ss);  
  63.         Matcher ma = null;  
  64.         ma = pa.matcher(html);  
  65.         String result = null;  
  66.         while(ma.find()){  
  67.             temp = ma.group();  
  68.             if(temp!=null){  
  69.                 if(temp.startsWith(">")){  
  70.                     temp = temp.substring(1);  
  71.                 }  
  72.                 if(temp.endsWith("<")){  
  73.                     temp = temp.substring(0, temp.length()-1);  
  74.                 }  
  75.                 if(!temp.equalsIgnoreCase("")){  
  76.                     if(result==null){  
  77.                         result = temp;  
  78.                     }  
  79.                     else{  
  80.                         result+="____"+temp;  
  81.                     }  
  82.                 }  
  83.             }  
  84.         }  
  85.         return result;  
  86.     }  
  87. }  

其中:GetContent用来获取标签内容,而GetLabel则用于获取标签。

实际上,这是正则法则运用中的一种。小编所运用到的这个正则法则的表达式是:

<[^>]+>:这个正则表达式可以匹配所有html标签,可以100%匹配,但需要注意页面编码方式和读取的编码方式。另外一个表达式是>[^<]+<,这个可以匹配标签内容。但由于小编对于正则法则不是非常的精通,并且时间有限,只研究出了这一种。另外用于设置网页自动提取标签的还有htmlparse、sax、dom4j等,但至于哪个更好用,哪个实现起来更容易,就要大家自己去探索了。