java版本爬虫

来源:互联网 发布:机械设计手册软件2016 编辑:程序博客网 时间:2024/05/01 00:42

<script type="text/javascript"><!--google_ad_client = "pub-5873492303276472";/* 728x90, 创建于 08-7-29 */google_ad_slot = "7502041044";google_ad_width = 728;google_ad_height = 90;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

这是一个web搜索的基本程序,从命令行输入搜索条件(起始的URL、处理url的最大数、要搜索的字符串),
它就会逐个对Internet上的URL进行实时搜索,查找并输出匹配搜索条件的页面。 这个程序的原型来自《java编程艺术》,
为了更好的分析,站长去掉了其中的GUI部分,并稍作修改以适用jdk1.5。以这个程序为基础,可以写出在互联网上搜索
诸如图像、邮件、网页下载之类的“爬虫”。
先请看程序运行的过程:
<script type="text/javascript"><!--google_ad_client = "pub-5873492303276472";/* 728x90, 08-7-29 */google_ad_slot = "7502041044";google_ad_width = 728;google_ad_height = 90;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

D:/java>javac  SearchCrawler.java(编译)

D:/java>java   SearchCrawler http://127.0.0.1:8080/zz3zcwbwebhome/index.jsp 20 java

Start searching...
result:
searchString=java
http://127.0.0.1:8080/zz3zcwbwebhome/index.jsp
http://127.0.0.1:8080/zz3zcwbwebhome/reply.jsp
http://127.0.0.1:8080/zz3zcwbwebhome/learn.jsp
http://127.0.0.1:8080/zz3zcwbwebhome/download.jsp
http://127.0.0.1:8080/zz3zcwbwebhome/article.jsp
http://127.0.0.1:8080/zz3zcwbwebhome/myexample/jlGUIOverview.htm
http://127.0.0.1:8080/zz3zcwbwebhome/myexample/Proxooldoc/index.html
http://127.0.0.1:8080/zz3zcwbwebhome/view.jsp?id=301
http://127.0.0.1:8080/zz3zcwbwebhome/view.jsp?id=297
http://127.0.0.1:8080/zz3zcwbwebhome/view.jsp?id=291
http://127.0.0.1:8080/zz3zcwbwebhome/view.jsp?id=286
http://127.0.0.1:8080/zz3zcwbwebhome/view.jsp?id=285
http://127.0.0.1:8080/zz3zcwbwebhome/view.jsp?id=284
http://127.0.0.1:8080/zz3zcwbwebhome/view.jsp?id=276
http://127.0.0.1:8080/zz3zcwbwebhome/view.jsp?id=272  

又如:
D:/java>java    SearchCrawler http://www.sina.com  20 java
Start searching...
result:
searchString=java
http://sina.com
http://redirect.sina.com/WWW/sinaCN/www.sina.com.cn class=a2
http://redirect.sina.com/WWW/sinaCN/www.sina.com.cn class=a8
http://redirect.sina.com/WWW/sinaHK/www.sina.com.hk class=a2
http://redirect.sina.com/WWW/sinaTW/www.sina.com.tw class=a8
http://redirect.sina.com/WWW/sinaUS/home.sina.com class=a8
http://redirect.sina.com/WWW/smsCN/sms.sina.com.cn/ class=a2
http://redirect.sina.com/WWW/smsCN/sms.sina.com.cn/ class=a3
http://redirect.sina.com/WWW/sinaNet/www.sina.net/ class=a3


D:/java>
下面是这个程序的源码

Java代码
  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. <script type="text/javascript"><!--google_ad_client = "pub-5873492303276472";/* 728x15, 08-7-29 */google_ad_slot = "7630759450";google_ad_width = 728;google_ad_height = 15;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
  93.         // 缓存此主机不允许访问的路径。   
  94.         disallowListCache.put(host, disallowList);   
  95.       } catch (Exception e) {   
  96.               return true//web站点根目录下没有robots.txt文件,返回真  
  97.       }   
  98.     }   
  99.   
  100.        
  101.     String file = urlToCheck.getFile();   
  102.     //System.out.println("文件getFile()="+file);  
  103.     for (int i = 0; i < disallowList.size(); i++) {   
  104.       String disallow = disallowList.get(i);   
  105.       if (file.startsWith(disallow)) {   
  106.         return false;   
  107.       }   
  108.     }   
  109.   
  110.     return true;   
  111.   }   
  112.   
  113.   
  114.   
  115.    
  116.   private String downloadPage(URL pageUrl) {  
  117.      try {  
  118.         // Open connection to URL for reading.  
  119.         BufferedReader reader =  
  120.           new BufferedReader(new InputStreamReader(pageUrl.openStream()));  
  121.   
  122.         // Read page into buffer.  
  123.         String line;  
  124.         StringBuffer pageBuffer = new StringBuffer();  
  125.         while ((line = reader.readLine()) != null) {  
  126.           pageBuffer.append(line);  
  127.         }  
  128.           
  129.         return pageBuffer.toString();  
  130.      } catch (Exception e) {  
  131.      }  
  132.   
  133.      return null;  
  134.   }  
  135.   
  136.   // 从URL中去掉"www"  
  137.   private String removeWwwFromUrl(String url) {  
  138.     int index = url.indexOf("://www.");  
  139.     if (index != -1) {  
  140.       return url.substring(0, index + 3) +  
  141.         url.substring(index + 7);  
  142.     }  
  143.   
  144.     return (url);  
  145.   }  
  146.   
  147.   // 解析页面并找出链接  
  148.   private ArrayList< String> retrieveLinks(URL pageUrl, String pageContents, HashSet crawledList,  
  149.     boolean limitHost)  
  150.   {  
  151.     // 用正则表达式编译链接的匹配模式。  
  152.     Pattern p =Pattern.compile("<a//s+href//s*=//s*/"?(.*?)[/"|>]",Pattern.CASE_INSENSITIVE);  
  153.     Matcher m = p.matcher(pageContents);  
  154.   
  155.       
  156.     ArrayList< String> linkList = new ArrayList< String>();  
  157.     while (m.find()) {  
  158.       String link = m.group(1).trim();  
  159.         
  160.       if (link.length() < 1) {  
  161.         continue;  
  162.       }  
  163.   
  164.       // 跳过链到本页面内链接。  
  165.       if (link.charAt(0) == '#') {  
  166.         continue;  
  167.       }  
  168.   
  169.         
  170.       if (link.indexOf("mailto:") != -1) {  
  171.         continue;  
  172.       }  
  173.        
  174.       if (link.toLowerCase().indexOf("javascript") != -1) {  
  175.         continue;  
  176.       }  
  177.   
  178.       if (link.indexOf("://") == -1){  
  179.         if (link.charAt(0) == '/') {//处理绝对地    
  180.           link = "http://" + pageUrl.getHost()+":"+pageUrl.getPort()+ link;  
  181.         } else {           
  182.           String file = pageUrl.getFile();  
  183.           if (file.indexOf('/') == -1) {//处理相对地址  
  184.             link = "http://" + pageUrl.getHost()+":"+pageUrl.getPort() + "/" + link;  
  185.           } else {  
  186.             String path =file.substring(0, file.lastIndexOf('/') + 1);  
  187.             link = "http://" + pageUrl.getHost() +":"+pageUrl.getPort()+ path + link;  
  188.           }  
  189.         }  
  190.       }  
  191.   
  192.       int index = link.indexOf('#');  
  193.       if (index != -1) {  
  194.         link = link.substring(0, index);  
  195.       }  
  196.   
  197.       link = removeWwwFromUrl(link);  
  198.   
  199.       URL verifiedLink = verifyUrl(link);  
  200.       if (verifiedLink == null) {  
  201.         continue;  
  202.       }  
  203.   
  204.       /* 如果限定主机,排除那些不合条件的URL*/  
  205.       if (limitHost &&  
  206.           !pageUrl.getHost().toLowerCase().equals(  
  207.             verifiedLink.getHost().toLowerCase()))  
  208.       {  
  209.         continue;  
  210.       }  
  211.   
  212.       // 跳过那些已经处理的链接.  
  213.       if (crawledList.contains(link)) {  
  214.         continue;  
  215.       }  
  216.   
  217.        linkList.add(link);  
  218.     }  
  219.   
  220.    return (linkList);  
  221.   }  
  222.   
  223.  // 搜索下载Web页面的内容,判断在该页面内有没有指定的搜索字符串  
  224.   
  225.   private boolean searchStringMatches(String pageContents, String searchString, boolean caseSensitive){  
  226.        String searchContents = pageContents;   
  227.        if (!caseSensitive) {//如果不区分大小写  
  228.           searchContents = pageContents.toLowerCase();  
  229.        }  
  230.   
  231.       
  232.     Pattern p = Pattern.compile("[//s]+");  
  233.     String[] terms = p.split(searchString);  
  234.     for (int i = 0; i < terms.length; i++) {  
  235.       if (caseSensitive) {  
  236.         if (searchContents.indexOf(terms[i]) == -1) {  
  237.           return false;  
  238.         }  
  239.       } else {  
  240.         if (searchContents.indexOf(terms[i].toLowerCase()) == -1) {  
  241.           return false;  
  242.         }  
  243.       }     }  
  244.   
  245.     return true;  
  246.   }  
  247.   
  248.     
  249.   //执行实际的搜索操作  
  250.   public ArrayList< String> crawl(String startUrl, int maxUrls, String searchString,boolean limithost,boolean caseSensitive )  
  251.   {   
  252.       
  253.     System.out.println("searchString="+searchString);  
  254.     HashSet< String> crawledList = new HashSet< String>();  
  255.     LinkedHashSet< String> toCrawlList = new LinkedHashSet< String>();  
  256.   
  257.      if (maxUrls < 1) {  
  258.         errorList.add("Invalid Max URLs value.");  
  259.         System.out.println("Invalid Max URLs value.");  
  260.       }  
  261.     
  262.       
  263.     if (searchString.length() < 1) {  
  264.       errorList.add("Missing Search String.");  
  265.       System.out.println("Missing search String");  
  266.     }  
  267.   
  268.       
  269.     if (errorList.size() > 0) {  
  270.       System.out.println("err!!!");  
  271.       return errorList;  
  272.       }  
  273.   
  274.       
  275.     // 从开始URL中移出www  
  276.     startUrl = removeWwwFromUrl(startUrl);  
  277.   
  278.       
  279.     toCrawlList.add(startUrl);  
  280.     while (toCrawlList.size() > 0) {  
  281.         
  282.       if (maxUrls != -1) {  
  283.         if (crawledList.size() == maxUrls) {  
  284.           break;  
  285.         }  
  286.       }  
  287.   
  288.       // Get URL at bottom of the list.  
  289.       String url =  toCrawlList.iterator().next();  
  290.   
  291.       // Remove URL from the to crawl list.  
  292.       toCrawlList.remove(url);  
  293.   
  294.       // Convert string url to URL object.  
  295.       URL verifiedUrl = verifyUrl(url);  
  296.   
  297.       // Skip URL if robots are not allowed to access it.  
  298.       if (!isRobotAllowed(verifiedUrl)) {  
  299.         continue;  
  300.       }  
  301.   
  302.       
  303.       // 增加已处理的URL到crawledList  
  304.       crawledList.add(url);  
  305.       String pageContents = downloadPage(verifiedUrl);  
  306.   
  307.         
  308.       if (pageContents != null && pageContents.length() > 0){  
  309.         // 从页面中获取有效的链接  
  310.         ArrayList< String> links =retrieveLinks(verifiedUrl, pageContents, crawledList,limitHost);  
  311.        
  312.         toCrawlList.addAll(links);  
  313.   
  314.         if (searchStringMatches(pageContents, searchString,caseSensitive))  
  315.         {  
  316.           result.add(url);  
  317.           System.out.println(url);  
  318.         }  
  319.      }  
  320.   
  321.       
  322.     }  
  323.    return result;  
  324.   }  
  325.   
  326.   // 主函数  
  327.   public static void main(String[] args) {  
  328.      if(args.length!=3){  
  329.         System.out.println("Usage:java SearchCrawler startUrl maxUrl searchString");  
  330.         return;  
  331.      }  
  332.     int max=Integer.parseInt(args[1]);  
  333.     SearchCrawler crawler = new SearchCrawler(args[0],max,args[2]);  
  334.     Thread  search=new Thread(crawler);  
  335.     System.out.println("Start searching...");  
  336.     System.out.println("result:");  
  337.     search.start();  
  338.      
  339.   }  

 
原创粉丝点击