夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評(píng)論 :: 0 Trackbacks
          今天晚上,幫我一個(gè)同門師兄,解決一下問題.
          題目是,抓取一個(gè)網(wǎng)站的所以頁面,并抓下這些頁碼的所有網(wǎng)址.
          代碼如下:
          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) 評(píng)論(0)  編輯  收藏 所屬分類: Regular Exp

          只有注冊用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 泗洪县| 宁安市| 聂拉木县| 靖宇县| 会宁县| 齐河县| 碌曲县| 东丰县| 淮南市| 武宣县| 白沙| 辽阳市| 平南县| 大方县| 兴宁市| 陆河县| 云霄县| 高淳县| 武邑县| 融水| 麻栗坡县| 仙桃市| 凤城市| 拜城县| 新兴县| 通化市| 洛隆县| 绥中县| 个旧市| 嵊泗县| 岐山县| 肥乡县| 水富县| 绥中县| 平邑县| 宜宾市| 平山县| 桦甸市| 慈利县| 炎陵县| 新津县|