97国产精品_亚洲欧美在线一区_五月综合婷婷色在线播放_婷婷综合久久中文字幕 - 欧美特大黄一级AA片片免费珠

020-85548809,29883069

網(wǎng)站設計、網(wǎng)站優(yōu)化、微信開發(fā)

資訊

分享你我感悟

您當前位置>主頁 > 資訊 > 網(wǎng)頁自動提取標簽欄如何設置

網(wǎng)頁自動提取標簽欄如何設置

     最近發(fā)現(xiàn)越來越多的網(wǎng)站喜歡使用自動提取標簽欄,如小編常去的糗事百科網(wǎng)站就有一個,截圖如下:

網(wǎng)頁自動提取標簽欄如何設置

 

如上圖。大家看右邊部分,搜索框下方就是一個自動提取的標簽欄。通過點擊這些出現(xiàn)頻率比較高的關鍵詞,用戶可以看到所有出現(xiàn)過此關鍵詞的帖子。結合站內搜索框來說,即成為一個強大的搜索工具。對于網(wǎng)站內容多,更新快,分類多等類型的網(wǎng)站來說,是比搜索框更便利的搜索工具,也將成為網(wǎng)頁設計的潮流趨勢之一。

雖然網(wǎng)頁設計越來越便捷,但是這背后設計師所付出的努力卻是艱辛的。我們看糗事百科一個看似很簡單的網(wǎng)站,但是如果請專業(yè)的網(wǎng)站建設公司來制作的話,成本最低都要好幾萬。這也能解釋為什么我們看起來不起眼的隨手的一個工具,說不定都凝聚了大批的設計師長時間的努力結果。

這個自動提取標簽的設計也是一樣。小編研究了一段時間,搜索了很多資料,才得出比較簡單一點的設置方法。實際上這個方法并非對所有的程序都有效,而只是對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%匹配,但需要注意頁面編碼方式和讀取的編碼方式。另外一個表達式是>[^<]+<,這個可以匹配標簽內容。但由于小編對于正則法則不是非常的精通,并且時間有限,只研究出了這一種。另外用于設置網(wǎng)頁自動提取標簽的還有htmlparse、sax、dom4j等,但至于哪個更好用,哪個實現(xiàn)起來更容易,就要大家自己去探索了。