Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063129
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: Java

2011-10-09 00:13:33

  1. import java.util.*;
  2. import java.net.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5. // 搜索Web爬行者

  6. public class SearchCrawler extends Thread{
  7.   private HashMap< String,ArrayList< String>> disallowListCache = new HashMap< String,ArrayList< String>>();
  8.   ArrayList< String> errorList= new ArrayList< String>();//错误信息

  9.   ArrayList< String> result=new ArrayList< String>(); //搜索到的结果

  10.   String startUrl;//开始搜索的起点

  11.   int maxUrl;//最大处理的url数

  12.   String searchString;//要搜索的字符串(英文)

  13.   boolean caseSensitive=false;//是否区分大小写

  14.   boolean limitHost=false;//是否在限制的主机内搜索

  15.   
  16.   public SearchCrawler(String startUrl,int maxUrl,String searchString){
  17.    this.startUrl=startUrl;
  18.    this.maxUrl=maxUrl;
  19.    this.searchString=searchString;
  20.   }
  21.    public ArrayList< String> getResult(){
  22.        return result;
  23.    }
  24.   public void run(){//启动搜索线程

  25.       
  26.        crawl(startUrl,maxUrl, searchString,limitHost,caseSensitive);
  27.   }
  28.    
  29.     //检测URL格式

  30.   private URL verifyUrl(String url) {
  31.     // 只处理HTTP URLs.

  32.     if (!url.toLowerCase().startsWith("http://"))
  33.       return null;
  34.     URL verifiedUrl = null;
  35.     try {
  36.       verifiedUrl = new URL(url);
  37.     } catch (Exception e) {
  38.       return null;
  39.     }
  40.     return verifiedUrl;
  41.   }

  42.   private String downloadPage(URL pageUrl) {
  43.      try {
  44.         // Open connection to URL for reading.

  45.         BufferedReader reader =
  46.           new BufferedReader(new InputStreamReader(pageUrl.openStream()));
  47.         // Read page into buffer.

  48.         String line;
  49.         StringBuffer pageBuffer = new StringBuffer();
  50.         while ((line = reader.readLine()) != null) {
  51.           pageBuffer.append(line);
  52.         }
  53.         
  54.         return pageBuffer.toString();
  55.      } catch (Exception e) {
  56.      }
  57.      return null;
  58.   }
  59.   // 从URL中去掉"www"

  60.   private String removeWwwFromUrl(String url) {
  61.     int index = url.indexOf("://www.");
  62.     if (index != -1) {
  63.       return url.substring(0, index + 3) +
  64.         url.substring(index + 7);
  65.     }
  66.     return (url);
  67.   }
  68.   // 解析页面并找出链接

  69.   private ArrayList< String> retrieveLinks(URL pageUrl, String pageContents, HashSet crawledList,
  70.     boolean limitHost)
  71.   {
  72.     // 用正则表达式编译链接的匹配模式。

  73.     //Pattern p =Pattern.compile("DOCTYPE \\s+ PUBLIC",Pattern.CASE_INSENSITIVE);

  74.     //Matcher m = p.matcher(pageContents);

  75.     
  76.     ArrayList< String> linkList = new ArrayList< String>();
  77.    // while (m.find()) {

  78.     int no=0;
  79.     while(true){
  80.       no = pageContents.indexOf("href=", no);
  81.       if(no<=0) break;
  82.       int startno=pageContents.indexOf("\"", no);
  83.       int stopno = pageContents.indexOf("\"", startno+1);
  84.       //String link = m.group(0);

  85.       no++;
  86.       String link = pageContents.substring(startno+1, stopno);
  87.       if (link.length() < 1) {
  88.         continue;
  89.       }
  90.       //跳过css

  91.       if (link.endsWith(".css")){
  92.          continue;
  93.       }
  94.       // 跳过链到本页面内链接。

  95.       if (link.charAt(0) == '#') {
  96.         continue;
  97.       }
  98.       
  99.       if (link.indexOf("mailto:") != -1) {
  100.         continue;
  101.       }
  102.      
  103.       if (link.toLowerCase().indexOf("javascript") != -1) {
  104.         continue;
  105.       }
  106.       if (link.indexOf("://") == -1){
  107.         if (link.charAt(0) == '/') {//处理绝对地

  108.             link = "http://" + pageUrl.getHost() + link;
  109.         } else {
  110.           String file = pageUrl.getFile();
  111.           if (file.indexOf('/') == -1) {//处理相对地址

  112.             link = "http://" + pageUrl.getHost()+":"+pageUrl.getPort() + "/" + link;
  113.           } else {
  114.             String path =file.substring(0, file.lastIndexOf('/') + 1);
  115.             link = "http://" + pageUrl.getHost() + link;
  116.           }
  117.         }
  118.       }
  119.       int index = link.indexOf('#');
  120.       if (index != -1) {
  121.         link = link.substring(0, index);
  122.       }
  123.       link = removeWwwFromUrl(link);
  124.       URL verifiedLink = verifyUrl(link);
  125.       if (verifiedLink == null) {
  126.         continue;
  127.       }
  128.       /* 如果限定主机,排除那些不合条件的URL*/
  129.       if (limitHost &&
  130.           !pageUrl.getHost().toLowerCase().equals(
  131.             verifiedLink.getHost().toLowerCase()))
  132.       {
  133.         continue;
  134.       }
  135.       // 跳过那些已经处理的链接.

  136.       if (crawledList.contains(link)) {
  137.         continue;
  138.       }
  139.        linkList.add(link);
  140.     }
  141.    return (linkList);
  142.   }
  143.  // 搜索下载Web页面的内容,判断在该页面内有没有指定的搜索字符串

  144.   private boolean searchStringMatches(String pageContents, String searchString, boolean caseSensitive){
  145.        String searchContents = pageContents;
  146.        if (!caseSensitive) {//如果不区分大小写

  147.           searchContents = pageContents.toLowerCase();
  148.        }
  149.     
  150.     Pattern p = Pattern.compile("[//s]+");
  151.     String[] terms = p.split(searchString);
  152.     for (int i = 0; i < terms.length; i++) {
  153.       if (caseSensitive) {
  154.         if (searchContents.indexOf(terms[i]) == -1) {
  155.           return false;
  156.         }
  157.       } else {
  158.         if (searchContents.indexOf(terms[i].toLowerCase()) == -1) {
  159.           return false;
  160.         }
  161.       } }
  162.     return true;
  163.   }
  164.   
  165.   //执行实际的搜索操作

  166.   public ArrayList< String> crawl(String startUrl, int maxUrls, String searchString,boolean limithost,boolean caseSensitive )
  167.   {
  168.     
  169.     System.out.println("searchString="+searchString);
  170.     HashSet< String> crawledList = new HashSet< String>();
  171.     LinkedHashSet< String> toCrawlList = new LinkedHashSet< String>();
  172.      if (maxUrls < 1) {
  173.         errorList.add("Invalid Max URLs value.");
  174.         System.out.println("Invalid Max URLs value.");
  175.       }
  176.   
  177.     
  178.     if (searchString.length() < 1) {
  179.       errorList.add("Missing Search String.");
  180.       System.out.println("Missing search String");
  181.     }
  182.     
  183.     if (errorList.size() > 0) {
  184.       System.out.println("err!!!");
  185.       return errorList;
  186.       }
  187.     
  188.     // 从开始URL中移出www

  189.     startUrl = removeWwwFromUrl(startUrl);
  190.     
  191.     toCrawlList.add(startUrl);
  192.     while (toCrawlList.size() > 0) {
  193.       
  194.       if (maxUrls != -1) {
  195.         if (crawledList.size() == maxUrls) {
  196.           break;
  197.         }
  198.       }
  199.       // Get URL at bottom of the list.

  200.       String url = toCrawlList.iterator().next();
  201.       // Remove URL from the to crawl list.

  202.       toCrawlList.remove(url);
  203.       // Convert string url to URL object.

  204.       URL verifiedUrl = verifyUrl(url);
  205.       // Skip URL if robots are not allowed to access it.

  206.      
  207.     
  208.       // 增加已处理的URL到crawledList

  209.       crawledList.add(url);
  210.       String pageContents = downloadPage(verifiedUrl);
  211.       
  212.       if (pageContents != null && pageContents.length() > 0){
  213.         // 从页面中获取有效的链接

  214.         ArrayList< String> links =retrieveLinks(verifiedUrl, pageContents, crawledList,limitHost);
  215.      
  216.         toCrawlList.addAll(links);
  217.         if (searchStringMatches(pageContents, searchString,caseSensitive))
  218.         {
  219.           result.add(url);
  220.           System.out.println(url);
  221.         }
  222.      }
  223.     
  224.     }
  225.    return result;
  226.   }
  227.   // 主函数

  228.   public static void main(String[] args) {
  229.     SearchCrawler crawler = new SearchCrawler("",10,".NET");
  230.     System.out.println("Start searching...");
  231.     System.out.println("result:");
  232.     crawler.start();
  233.   }
  234. }
阅读(887) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~