夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          今天晚上,幫我一個同門師兄,解決一下問題.
          題目是,抓取一個網站的所以頁面,并抓下這些頁碼的所有網址.
          代碼如下:
          package com.hwp.test;

          import java.io.InputStream;
          import java.net.HttpURLConnection;
          import java.net.URL;
          import java.util.ArrayList;
          import java.util.HashMap;
          import java.util.List;
          import java.util.Map;
          import java.util.Set;
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;

          public class SearchEngine
          {
              
          private Map<String, List<String>> pageNameUrls;
              
              
          public SearchEngine()
              
          {
                  pageNameUrls 
          = new HashMap<String, List<String>>();
              }

              
              
          private String getContent(String httpUrl)
              
          {
                  String htmlCode 
          = "";
                  
          try
                  
          {
                      InputStream in;
                      URL url 
          = new java.net.URL(httpUrl);
                      HttpURLConnection connection 
          = (HttpURLConnection) url
                              .openConnection();
                      connection 
          = (HttpURLConnection) url.openConnection();
                      connection.setRequestProperty(
          "User-Agent""Mozilla/4.0");
                      connection.connect();
                      in 
          = connection.getInputStream();
                      
          byte[] buffer = new byte[512];
                      
          int length = -1;
                      
          while ((length = in.read(buffer, 0512)) != -1)
                      
          {
                          htmlCode 
          += new String(buffer, 0, length);
                      }

                  }

                  
          catch (Exception e)
                  
          {}
                  
          if (htmlCode == null)
                  
          {
                      
          return "";
                  }

                  
          return htmlCode;
              }

              
              
          private List<String> getPageUrls(String page)
              
          {
                  List
          <String> urls = new ArrayList<String>();
                  String content 
          = this.getContent(page);
                   
                  String reg 
          = "http://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
                  Pattern pattern 
          = Pattern.compile(reg);
                  Matcher matcher 
          = pattern.matcher(content);
                  
                  String url 
          = "";
                  
          while (matcher.find())
                  
          {
                      url 
          = matcher.group();
                      
          if (!urls.contains(url))
                      
          {
                          urls.add(url);
                      }

                  }

                  
          return urls;
              }

              
              
          public void test(String url, String baseUrl)
              
          {
                  String content 
          = this.getContent(url);
                  
          // System.out.println(content);
                  String reg = "(" + baseUrl
                          
          + "(/[\\w-]+)*(/[\\w-]+\\.(htm|html|xhtml|jsp|asp|php)))";
                  Pattern pattern 
          = Pattern.compile(reg);
                  Matcher matcher 
          = pattern.matcher(content);
                  
          while (matcher.find())
                  
          {
                      String tempUrl 
          = matcher.group();
                      
          if (!this.pageNameUrls.containsKey(tempUrl))
                      
          {
                          
          //System.out.println(tempUrl);
                          this.pageNameUrls.put(tempUrl, this.getPageUrls(tempUrl));
                          test(tempUrl, baseUrl);
                      }

                  }

              }

              
              
          public static void main(String[] args)
              
          {
                  String url 
          = "http://www.aygfsteel.com";
                  String baseUrl 
          = "http://www.aygfsteel.com";
                  SearchEngine se 
          = new SearchEngine();
                  se.test(url, baseUrl);
                  Map
          <String, List<String>> map= se.pageNameUrls;
                  Set
          <Map.Entry<String, List<String>>> set = map.entrySet();
                  
          for(Map.Entry<String, List<String>> entry: set)
                  
          {
                      System.out.println(entry.getKey());
                      System.out.println(entry.getValue());
                  }

              }

          }

          posted on 2008-07-14 23:24 HUIKK 閱讀(405) 評論(0)  編輯  收藏 所屬分類: Regular Exp

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 龙山县| 汝阳县| 会泽县| 宣威市| 大理市| 禹州市| 盖州市| 义马市| 银川市| 天长市| 弋阳县| 郧西县| 石门县| 周至县| 托克逊县| 康平县| 新巴尔虎右旗| 富宁县| 清新县| 桦南县| 华阴市| 淅川县| 湘西| 九龙坡区| 阿鲁科尔沁旗| 高尔夫| 偃师市| 沧源| 沙洋县| 丰宁| 于都县| 眉山市| 阿克苏市| 朔州市| 苏州市| 霍邱县| 盱眙县| 正阳县| 湖南省| 石阡县| 临澧县|