`
zhaohaolin
  • 浏览: 986616 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Heritrix3.0教程 使用入门(四) 载入种子的四种方式

阅读更多

Heritrix3.0新特性一大亮点就是,相比以前版本载入种子更灵活(甚至你可以动态载入种子),同时可以载入N个种子.以前版本载入种子是全部加载到内存

,而一旦种子过多,那容易导致内存溢出.而新版本会分批次写入硬盘(通过调度器写入).所以避免了这个问题.下面就说一下Heritrix3.0

载入种子的四种方式,分别是:直接载入,通过seeds.txt载入,通过ActionDirectory动态载入(注意是动态,你随时可以载入),自定义载入.
1. 直接载入 :
直接载入比较方便,只需直接在crawler.beans.cxml中设置就可以,具体设置如下.

  1. <bean  class = "org.archive.modules.seeds.TextSeedModule" >  
  2. <property>  
  3. <bean>  
  4. <property>  
  5. <value>  
  6. # your seeds  
  7. </value>  
  8. </property>  
  9. </bean>  
  10. </property>  
  11. -->  
  12. <!-- <property name='sourceTagSeeds'  value= 'false' /> -->  
  13. <!--  
  14. </bean>  


其中只要把your seeds替换成你想要抓取的URL即可,如http://www.yun5u.com.其中sourceTagSeeds表示是否让新抽取出来的URL记录它的种子.
这个最方便,缺点就是如果种子太多,会使得crawler-beans.xml文件过于庞大,不美观
2. 通过 seeds.txt 载入
也只需要在crawler.beans.cxml中配置,同时将种子填充到seeds.txt中即可:

  1. <bean  class = "org.archive.modules.seeds.TextSeedModule" >  
  2. <property>  
  3. <bean>  
  4. <property value="seeds.txt"  />  
  5. </bean>  
  6. </property>  
  7. <property name='sourceTagSeeds'  value= 'false' />  
  8. </bean>  


也很方便,同时保持了crawler-beans.cxml的美观.缺点其实1)也有,没有动态性,不能自定义扩展.比如想从数据库导入,甚至加上自己的属性.
3. 通过 ActionDirectory 动态载入
我觉得ActionDirectory是Heritrix3.0最有作用的新特性.因为可以动态控制抓取.比如想新增抓取的种子,想临时存放一些你不想抓取的URL,以及
临时让Heritrix抓取一些URL.ActionDirectory都可以很好的满足.我想这个还是以后要着重介绍下.下面先介绍动态载入种子的适用方法:
首先需要在crawler-beans.cxml中配置ActionDirectory:

  1. <bean  class = "org.archive.crawler.framework.ActionDirectory" >  
  2. <property name="actionDir"  value= "action"  />  
  3. <property name="initialDelaySeconds"  value= "10"  />  
  4. <property name="delaySeconds"  value= "30"  />  
  5. </bean>  


其中actionDir表示你的action目录,你得把动态加载的URL文件放到该目录下.默认为action,表示在Heritrix运行的目录下.
initialDelaySeconds表示,初始化时需要延迟的时间,这里是10秒
delaySeconds表示定时扫描actionDir的间隔时间,这里是30秒.
其中启动actionDirectory这个bean并且运行Heritrix3.0之后,你就会在你的抓取目录下看到action这个目录.当然这里你也可以自己指定.
随后可以生成一个文件,但必须以.seeds或.seeds(.gz)结尾.Heritrix会把以这两种文件名结尾的文件当做种子文件来处理.其中.seeds(.gz)
表示压缩文件.将你的种子写入.seeds文件,如test.seeds,再放入action目录,30秒后会发现该文件转移到./action/done目录下,格式为时间.test.seeds.
这个时候就表示test.seeds这个种子文件已经被加载了.你再观察下控制台,或者crawl.log就有可能发现你的种子已经被处理了.
当然,这种载入方式,你可以根据你的需要随时运行.
4. 自定义载入 .
相信很多人,都有自定义载入种子的这个需求.比如从数据库中载入,或者加上自己的属性.这个就要扩展Heritrix的SeedModule了.我写了一个示例,代码如下:

  1. import java.io.File;  
  2. import java.util.Vector;  
  3. import org.apache.commons.httpclient.URIException;  
  4. import org.archive.modules.CrawlURI;  
  5. import org.archive.modules.SchedulingConstants;  
  6. import org.archive.modules.seeds.SeedModule;  
  7. import com.crawl.config.CrawlGlobal;  
  8. import com.crawl.controller.MyHeritrixController;  
  9. public   class  MySeedModule  extends  SeedModule {  
  10. private   static  final long serialVersionUID = 1L;  
  11. public   static  MyHeritrixController mhc=MyHeritrixController.getInstance();  // 单例模式,实现获得种子   
  12. /**  
  13. * 对ActionDirectory下.seeds或.seeds.gz的处理  
  14. */   
  15. @Override  
  16. public  void actOn(File f) {  
  17. }  
  18. /**  
  19. * 添加种子,比如动态添加了种子,这里既要将其载入,又要将其记录下来  
  20. */   
  21. @Override  
  22. public  void addSeed(CrawlURI curi) {  
  23. }  
  24. /**  
  25. * 载入种子  
  26. */   
  27. @Override  
  28. public  void announceSeeds() {  
  29. Vector seeds=mhc.getSeeds(); // 获得种子,这里需要根据你的需要自己实现.   
  30. String url=null;  
  31. CrawlURI curi=null;  
  32. try {  
  33. for (int i=0; i< seeds .size();i++){ url=seeds.get(i);  if (url!=null&&!url.equals( "" )){ curi=HeritrixUrlUtil.urlToCrawlURI(url, SchedulingConstants.MEDIUM);  // 自 己实现的方法,将String类型的URL转换为CrawlURI curi.setSeed(true); // 设置为种 子 curi.setSourceTag(curi.toString()); // 记录该URL来源的种子 URL curi.getData().put(CrawlGlobal.SITE_ID,new Integer(i) ); // 自定义属 性 curi.makeHeritable(CrawlGlobal.SITE_ID); // 持久化自定义属 性 curi.setForceFetch(true); // 设置为强制抓取,已经抓取过也继续抓 取 publishAddedSeed(curi); // 提交种 子 } } } catch (URIException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }   

 

  1. // HeritrixUrlUtil.urlToCrawlURI(url, SchedulingConstants.MEDIUM); 代码   
  2. /**  
  3. * 将字符串形式的URL转换为CrawlURI  
  4. * @param url  
  5. * @param priority  
  6. * @return  
  7. * @throws URIException  
  8. */   
  9. public   static  CrawlURI urlToCrawlURI(String url,int priority) throws URIException{  
  10. if  (!url.matches( "[a-zA-Z][\\w+\\-]+:.*" )) {  
  11. url = "http://"  + url;  
  12. }  
  13. url=url.replace("&" "&" );  
  14. UURI uuri=UURIFactory.getInstance(url);  
  15. CrawlURI curi=new  CrawlURI(uuri);  
  16. curi.setSchedulingDirective(priority);  
  17. return  curi;  
  18. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics