Heritrix 抓取 高级篇

使用Heritrix进行抓取网页,有半天阅读我之前博客的话,很容易就能够顺利的进行抓取任务,但在抓取过程中可能会遇到: 
1 想抓取特定格式/特定要求 的网页  
这个要根据具体的网站,才能采取具体的措施。这主要是根据网站编写的时候,它的出度的具体格式。如果是类似<a href="http://www.xxx.xxx.xx...." ..>这样的可以直接指向某个具体的URL,那么添加到URI中的应该是这个完整的URL,如果是去掉了http://www等的前面的内容,而只是简单指向本网站下的某个网页,那么在加入到URI中的时候,要记得加上头使得它是一个完整的网页的URL。根据CCER网站下的网页内容,自己写了个CCERExtractor.java来进行过滤,只抓取符合条件的URL。 
Java代码   收藏代码
  1. package org.archive.crawler.extractor;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.regex.Matcher;  
  5. import java.util.regex.Pattern;  
  6.   
  7. import org.apache.commons.httpclient.URIException;  
  8. import org.archive.crawler.datamodel.CrawlURI;  
  9. import org.archive.io.ReplayCharSequence;  
  10. import org.archive.util.HttpRecorder;  
  11.   
  12. /** 
  13.  * I ignore the log information. 
  14.  * @author Administrator 
  15.  * 
  16.  */  
  17. public class CCERExtractor extends Extractor{  
  18.       
  19.     /** 
  20.      * if the url starts with http          ----        if it is under http://www.pku.edu.cn, not then "not" 
  21.      * else if matches mailto or javascript ----        "not" 
  22.      * else                                 ----        "yes" 
  23.      */  
  24.     public static final String pattern_ahref = "<[aA] href=\"([^\"]+)\"";// group(1)  
  25.       
  26.     public CCERExtractor(String name){  
  27.         super(name,"CCER Extractor");  
  28.     }  
  29.       
  30.     public CCERExtractor(String name, String description) {  
  31.         super(name, description);  
  32.     }  
  33.   
  34.     @Override  
  35.     protected void extract(CrawlURI curi) {  
  36.         HttpRecorder hr = curi.getHttpRecorder();  
  37.         ReplayCharSequence cs = null;  
  38.         try {  
  39.             cs = hr.getReplayCharSequence();  
  40.         } catch (IOException e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.         if(cs == null){  
  44.             return;  
  45.         }  
  46.           
  47.         String content = cs.toString();  
  48.         Matcher matcher = Pattern.compile(CCERExtractor.pattern_ahref).matcher(content);  
  49.         while(matcher.find()){  
  50.             String newUrl = matcher.group(1);  
  51.             if(newUrl.startsWith("http")){//find the ccer website  
  52.                 if(newUrl.startsWith("http://www.pku.edu.cn")){// case 1 that matches  
  53.                     createAndAddLinkRelativeToBase(curi, newUrl, Link.NAVLINK_HOP);  
  54.                 }  
  55.             }else if(!newUrl.toLowerCase().startsWith("mailto") && !newUrl.toLowerCase().startsWith("javascript")){//case 2 that matches. Ignore the mailto and javascript href.   
  56.                 if(newUrl.trim().startsWith("/")){  
  57.                     newUrl = newUrl.trim().substring(1).trim();  
  58.                 }  
  59.                 newUrl = "http://www.ccer.pku.edu.cn/cn/" + newUrl;//" http://www.ccer.pku.edu.cn/cn/ " should be added to the first  
  60.                 createAndAddLinkRelativeToBase(curi, newUrl, Link.NAVLINK_HOP);// make sure that the newUrl is available.  
  61.             }  
  62.         }  
  63.     }  
  64.   
  65.     private void createAndAddLinkRelativeToBase(CrawlURI curi, String newUrl, char hopType){  
  66.         try {  
  67.             curi.createAndAddLinkRelativeToBase(newUrl, "", hopType);  
  68.         } catch (URIException e) {  
  69.             e.printStackTrace();  
  70.         }  
  71.     }  
  72. }  


在modules下的Processor.options下将这个新的解析器加入进去,那么在配置的时候便可以出现这个选项。但是必须注意: Crawler是严格按照配置的信息来进行抓取的,所以CCERExtractor必须在ExtractorHttp后面 。在options里面的位置无所谓,只要放到extractor中即可,没有先后顺序。 



2 单线程的困扰 (我之前的博客 http://hanyuanbo.iteye.com/blog/788177

3 机器内存的限制 (在run configuration中,设置VM-arguments为-Xmx512m。见附件) 

4 robots.txt的限制  
这个是“君子协定”的内容,如果不考虑这个限制的话,可以提高抓取的效率(虽然某些网站可能不希望你这样肆无忌惮的抓取它的网站)。这个很简单,只需要把 
Java代码   收藏代码
  1. org.archive.crawler.prefetch.PreconditionEnforcer  
中将 
Java代码   收藏代码
  1. private boolean considerRobotsPreconditions(CrawlURI curi) {  
  2.          ...  
  3. }  
改成 
Java代码   收藏代码
  1. private boolean considerRobotsPreconditions(CrawlURI curi) {  
  2.         return false;//无论如何都不考虑robots.txt的限制  
  3. }  

5 有时候配置的时候,那个可选项的下拉菜单没有了!! (在Heritrix的run configuration中,classpath中的user entries中选择右边的advanced,然后选择external folder,选择conf目录即可。见附件) 


至此,便开始抓取啦,还是按照之前博客里所说的配置,记得在选择extractor的时候,选择CCERExtractor,并且这个要放在ExtractorHtml后面。抓取过程如下: 








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值