Java写的爬虫的基本程序

这是一个web搜索的基本程序,从命令行输入搜索条件(起始的URL、处理url的最大数、要搜索的字符串),
它就会逐个对Internet上的URL进行实时搜索,查找并输出匹配搜索条件的页面。 这个程序的原型来自《java编程艺术》,
为了更好的分析,站长去掉了其中的GUI部分,并稍作修改以适用jdk1.5。以这个程序为基础,可以写出在互联网上搜索
诸如图像、邮件、网页下载之类的“爬虫”。

下面是这个程序的源码


  1. import java.util.*;  
  2. import java.net.*;  
  3. import java.io.*;  
  4. import java.util.regex.*;  
  5.   
  6. // 搜索Web爬行者  
  7. public class SearchCrawler implements Runnable{  
  8.    
  9. /* disallowListCache缓存robot不允许搜索的URL。 Robot协议在Web站点的根目录下设置一个robots.txt文件,
  10.    *规定站点上的哪些页面是限制搜索的。 搜索程序应该在搜索过程中跳过这些区域,下面是robots.txt的一个例子:
  11. # robots.txt for http://somehost.com/
  12.     User-agent: *
  13.     Disallow: /cgi-bin/
  14.     Disallow: /registration # /Disallow robots on registration page
  15.     Disallow: /login
  16.    */  
  17.   
  18.   
  19.   private HashMap< String,ArrayList< String>> disallowListCache = new HashMap< String,ArrayList< String>>();   
  20.    ArrayList< String> errorList= new ArrayList< String>();//错误信息   
  21.    ArrayList< String> result=new ArrayList< String>(); //搜索到的结果   
  22.    String startUrl;//开始搜索的起点  
  23.   int maxUrl;//最大处理的url数  
  24.    String searchString;//要搜索的字符串(英文)  
  25.   boolean caseSensitive=false;//是否区分大小写  
  26.   boolean limitHost=false;//是否在限制的主机内搜索  
  27.     
  28.   public SearchCrawler(String startUrl,int maxUrl,String searchString){  
  29.    this.startUrl=startUrl;  
  30.    this.maxUrl=maxUrl;  
  31.    this.searchString=searchString;  
  32.    }  
  33.   
  34.    public ArrayList< String> getResult(){  
  35.        return result;  
  36.     }  
  37.   
  38.   public void run(){//启动搜索线程  
  39.         
  40.         crawl(startUrl,maxUrl, searchString,limitHost,caseSensitive);  
  41.    }  
  42.      
  43.   
  44.     //检测URL格式  
  45.   private URL verifyUrl(String url) {  
  46.     // 只处理HTTP URLs.  
  47.     if (!url.toLowerCase().startsWith("http://"))  
  48.       return null;  
  49.   
  50.      URL verifiedUrl = null;  
  51.     try {  
  52.        verifiedUrl = new URL(url);  
  53.      } catch (Exception e) {  
  54.       return null;  
  55.      }  
  56.   
  57.     return verifiedUrl;  
  58.    }  
  59.   
  60.   // 检测robot是否允许访问给出的URL.  
  61. private boolean isRobotAllowed(URL urlToCheck) {   
  62.      String host = urlToCheck.getHost().toLowerCase();//获取给出RUL的主机   
  63.     //System.out.println("主机="+host);  
  64.   
  65.     // 获取主机不允许搜索的URL缓存   
  66.      ArrayList< String> disallowList =disallowListCache.get(host);   
  67.   
  68.     // 如果还没有缓存,下载并缓存。   
  69.     if (disallowList == null) {   
  70.        disallowList = new ArrayList< String>();   
  71.       try {   
  72.          URL robotsFileUrl =new URL("http://" + host + "/robots.txt");   
  73.          BufferedReader reader =new BufferedReader(new InputStreamReader(robotsFileUrl.openStream()));   
  74.   
  75.         // 读robot文件,创建不允许访问的路径列表。   
  76.          String line;   
  77.         while ((line = reader.readLine()) != null) {   
  78.           if (line.indexOf("Disallow:") == 0) {//是否包含"Disallow:"   
  79.              String disallowPath =line.substring("Disallow:".length());//获取不允许访问路径   
  80.   
  81.             // 检查是否有注释。   
  82.             int commentIndex = disallowPath.indexOf("#");   
  83.             if (commentIndex != - 1) {   
  84.                disallowPath =disallowPath.substring(0, commentIndex);//去掉注释   
  85.              }   
  86.                
  87.              disallowPath = disallowPath.trim();   
  88.              disallowList.add(disallowPath);   
  89.             }   
  90.           }   
  91.   
  92.         // 缓存此主机不允许访问的路径。   
  93.          disallowListCache.put(host, disallowList);   
  94.        } catch (Exception e) {   
  95.               return true; //web站点根目录下没有robots.txt文件,返回真  
  96.        }   
  97.      }   
  98.   
  99.        
  100.      String file = urlToCheck.getFile();   
  101.     //System.out.println("文件getFile()="+file);  
  102.     for (int i = 0; i < disallowList.size(); i++) {   
  103.        String disallow = disallowList.get(i);   
  104.       if (file.startsWith(disallow)) {   
  105.         return false;   
  106.        }   
  107.      }   
  108.   
  109.     return true;   
  110.    }   
  111.   
  112.   
  113.   
  114.    
  115.   private String downloadPage(URL pageUrl) {  
  116.      try {  
  117.         // Open connection to URL for reading.  
  118.          BufferedReader reader =  
  119.           new BufferedReader(new InputStreamReader(pageUrl.openStream()));  
  120.   
  121.         // Read page into buffer.  
  122.          String line;  
  123.          StringBuffer pageBuffer = new StringBuffer();  
  124.         while ((line = reader.readLine()) != null) {  
  125.            pageBuffer.append(line);  
  126.          }  
  127.           
  128.         return pageBuffer.toString();  
  129.       } catch (Exception e) {  
  130.       }  
  131.   
  132.      return null;  
  133.    }  
  134.   
  135.   // 从URL中去掉"www"  
  136.   private String removeWwwFromUrl(String url) {  
  137.     int index = url.indexOf("://www.");  
  138.     if (index != -1) {  
  139.       return url.substring(0, index + 3) +  
  140.          url.substring(index + 7);  
  141.      }  
  142.   
  143.     return (url);  
  144.    }  
  1.  // 解析页面并找出链接  
  2.   private ArrayList< String> retrieveLinks(URL pageUrl, String pageContents, HashSet crawledList,  
  3.     boolean limitHost)  
  4.    {  
  5.     // 用正则表达式编译链接的匹配模式。  
  6.   
  7.      Pattern p =Pattern.compile("<a//s+href//s*=//s*/"?(.*?)[/"|>]",Pattern.CASE_INSENSITIVE);  
  8.      Matcher m = p.matcher(pageContents);  
  9.   
  10.       
  11.      ArrayList< String> linkList = new ArrayList< String>();  
  12.     while (m.find()) {  
  13.        String link = m.group(1).trim();  
  14.         
  15.       if (link.length() < 1) {  
  16.         continue;  
  17.        }  
  18.   
  19.       // 跳过链到本页面内链接。  
  20.       if (link.charAt(0) == '#') {  
  21.         continue;  
  22.        }  
  23.   
  24.         
  25.       if (link.indexOf("mailto:") != -1) {  
  26.         continue;  
  27.        }  
  28.        
  29.       if (link.toLowerCase().indexOf("javascript") != -1) {  
  30.         continue;  
  31.        }  
  32.   
  33.       if (link.indexOf("://") == -1){  
  34.         if (link.charAt(0) == '/') {//处理绝对地    
  35.            link = "http://" + pageUrl.getHost()+":"+pageUrl.getPort()+ link;  
  36.          } else {           
  37.            String file = pageUrl.getFile();  
  38.           if (file.indexOf('/') == -1) {//处理相对地址  
  39.              link = "http://" + pageUrl.getHost()+":"+pageUrl.getPort() + "/" + link;  
  40.            } else {  
  41.              String path =file.substring(0, file.lastIndexOf('/') + 1);  
  42.              link = "http://" + pageUrl.getHost() +":"+pageUrl.getPort()+ path + link;  
  43.            }  
  44.          }  
  45.        }  
  46.   
  47.       int index = link.indexOf('#');  
  48.       if (index != -1) {  
  49.          link = link.substring(0, index);  
  50.        }  
  51.   
  52.        link = removeWwwFromUrl(link);  
  53.   
  54.        URL verifiedLink = verifyUrl(link);  
  55.       if (verifiedLink == null) {  
  56.         continue;  
  57.        }  
  58.   
  59.       /* 如果限定主机,排除那些不合条件的URL*/  
  60.       if (limitHost &&  
  61.            !pageUrl.getHost().toLowerCase().equals(  
  62.              verifiedLink.getHost().toLowerCase()))  
  63.        {  
  64.         continue;  
  65.        }  
  66.   
  67.       // 跳过那些已经处理的链接.  
  68.       if (crawledList.contains(link)) {  
  69.         continue;  
  70.        }  
  71.   
  72.         linkList.add(link);  
  73.      }  
  74.   
  75.    return (linkList);  
  76.    }  
  77.   
  78. // 搜索下载Web页面的内容,判断在该页面内有没有指定的搜索字符串  
  79.   
  80.   private boolean searchStringMatches(String pageContents, String searchString, boolean caseSensitive){  
  81.         String searchContents = pageContents;   
  82.        if (!caseSensitive) {//如果不区分大小写  
  83.            searchContents = pageContents.toLowerCase();  
  84.         }  
  85.   
  86.       
  87.      Pattern p = Pattern.compile("[//s]+");  
  88.      String[] terms = p.split(searchString);  
  89.     for (int i = 0; i < terms.length; i++) {  
  90.       if (caseSensitive) {  
  91.         if (searchContents.indexOf(terms[i]) == -1) {  
  92.           return false;  
  93.          }  
  94.        } else {  
  95.         if (searchContents.indexOf(terms[i].toLowerCase()) == -1) {  
  96.           return false;  
  97.          }  
  98.        }      }  
  99.   
  100.     return true;  
  101.    }  
  102.   
  103.     
  104.   //执行实际的搜索操作  
  105.   public ArrayList< String> crawl(String startUrl, int maxUrls, String searchString,boolean limithost,boolean caseSensitive )  
  106.    {   
  107.       
  108.      System.out.println("searchString="+searchString);  
  109.      HashSet< String> crawledList = new HashSet< String>();  
  110.      LinkedHashSet< String> toCrawlList = new LinkedHashSet< String>();  
  111.   
  112.      if (maxUrls < 1) {  
  113.          errorList.add("Invalid Max URLs value.");  
  114.          System.out.println("Invalid Max URLs value.");  
  115.        }  
  116.     
  117.       
  118.     if (searchString.length() < 1) {  
  119.        errorList.add("Missing Search String.");  
  120.        System.out.println("Missing search String");  
  121.      }  
  122.   
  123.       
  124.     if (errorList.size() > 0) {  
  125.        System.out.println("err!!!");  
  126.       return errorList;  
  127.        }  
  128.   
  129.       
  130.     // 从开始URL中移出www  
  131.      startUrl = removeWwwFromUrl(startUrl);  
  132.   
  133.       
  134.      toCrawlList.add(startUrl);  
  135.     while (toCrawlList.size() > 0) {  
  136.         
  137.       if (maxUrls != -1) {  
  138.         if (crawledList.size() == maxUrls) {  
  139.           break;  
  140.          }  
  141.        }  
  142.   
  143.       // Get URL at bottom of the list.  
  144.        String url =   toCrawlList.iterator().next();  
  145.   
  146.       // Remove URL from the to crawl list.  
  147.        toCrawlList.remove(url);  
  148.   
  149.       // Convert string url to URL object.  
  150.        URL verifiedUrl = verifyUrl(url);  
  151.   
  152.       // Skip URL if robots are not allowed to access it.  
  153.       if (!isRobotAllowed(verifiedUrl)) {  
  154.         continue;  
  155.        }  
  156.   
  157.       
  158.       // 增加已处理的URL到crawledList  
  159.        crawledList.add(url);  
  160.        String pageContents = downloadPage(verifiedUrl);  
  161.   
  162.         
  163.       if (pageContents != null && pageContents.length() > 0){  
  164.         // 从页面中获取有效的链接  
  165.          ArrayList< String> links =retrieveLinks(verifiedUrl, pageContents, crawledList,limitHost);  
  166.        
  167.          toCrawlList.addAll(links);  
  168.   
  169.         if (searchStringMatches(pageContents, searchString,caseSensitive))  
  170.          {  
  171.            result.add(url);  
  172.            System.out.println(url);  
  173.          }  
  174.       }  
  175.   
  176.       
  177.      }  
  178.    return result;  
  179.    }  
  180.   
  181.   // 主函数  
  182.   public static void main(String[] args) {  
  183.      if(args.length!=3){  
  184.          System.out.println("Usage:java SearchCrawler startUrl maxUrl searchString");  
  185.         return;  
  186.       }  
  187.     int max=Integer.parseInt(args[1]);  
  188.      SearchCrawler crawler = new SearchCrawler(args[0],max,args[2]);  
  189.      Thread   search=new Thread(crawler);  
  190.      System.out.println("Start searching...");  
  191.      System.out.println("result:");  
  192.      search.start();  
  193.      
  194.    }  
  195. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值