heritrix 提高抓取速度

最近一直用heritrix爬取网站,  晚上heritrix一直运行着, 但奇怪的是heritrix 抓取速度非常慢, 抓取一个网站, 用了8个多小时, 竟然没有运行完。 于是根据LOG 分析了一下慢的原因

 

Java代码   收藏代码
  1.  -----===== SNOOZED QUEUES =====-----  
  2. SNOOZED#0:  
  3. Queue us,imageshack,img245,+2 (p1)  
  4.   1 items  
  5.    wakes in: 99m19s74ms  
  6.     last enqueued: <a href="http://img245.xxx.us/img245/596/193183637x01ss500sclzzzbx0.jpg">http://img245.xxx.us/img245/596/193183637x01ss500sclzzzbx0.jpg  
  7. </a>      last peeked: <a href="http://img245.xxxx.us/img245/596/193183637x01ss500sclzzzbx0.jpg">http://img245.xxxx.us/img245/596/193183637x01ss500sclzzzbx0.jpg  
  8. </a>   total expended: 12 (total budget: -1)  
  9.    active balance: 2988  
  10.    last(avg) cost: 1(1)  
  11.    totalScheduled fetchSuccesses fetchFailures fetchDisregards fetchResponses robotsDenials successBytes totalBytes fetchNonResponses  
  12.    2 1 0 0 1 0 59 59 12  
  13.    SimplePrecedenceProvider  
  14.    1  

 SNOOZED QUene 里面有一些图片一直在那里, 并且运行时间相当长,

用浏览器打开, 那图片不存在,于是那图片一直在QUENE当中。

 

接着我分析了一下heritrix 中代码:

 

workQueneFrontier 有下面代码, 由于图片不存在会进入needsRetrying代码块中。

Java代码   收藏代码
  1. if (needsRetrying(curi)) {  
  2.     // Consider errors which can be retried, leaving uri atop queue  
  3.     if(curi.getFetchStatus()!=S_DEFERRED) {  
  4.         wq.expend(curi.getHolderCost()); // all retries but DEFERRED cost  
  5.     }  
  6.     long delay_sec = retryDelayFor(curi);  
  7.     curi.processingCleanup(); // lose state that shouldn't burden retry  
  8.   
  9.         wq.unpeek(curi);  
  10.         // TODO: consider if this should happen automatically inside unpeek()  
  11.         wq.update(this, curi); // rewrite any changes  
  12.         if (delay_sec > 0) {  
  13.             long delay_ms = delay_sec * 1000;  
  14.             snoozeQueue(wq, now, delay_ms);  
  15.         } else {  
  16.             reenqueueQueue(wq);  
  17.         }  
  18.   
  19.     // Let everyone interested know that it will be retried.  
  20.     appCtx.publishEvent(  
  21.         new CrawlURIDispositionEvent(this,curi,DEFERRED_FOR_RETRY));  
  22.     doJournalRescheduled(curi);  
  23.     return;  
  24. }  

 

 

 

 retryDelayFor方法是用来抓取失败, 计算等待的时间, 代码于如下

 

Java代码   收藏代码
  1. /** 
  2.  * Return a suitable value to wait before retrying the given URI. 
  3.  *  
  4.  * @param curi 
  5.  *            CrawlURI to be retried 
  6.  * @return millisecond delay before retry 
  7.  */  
  8. protected long retryDelayFor(CrawlURI curi) {  
  9.     int status = curi.getFetchStatus();  
  10.     return (status == S_CONNECT_FAILED || status == S_CONNECT_LOST ||  
  11.             status == S_DOMAIN_UNRESOLVABLE)? getRetryDelaySeconds() : 0;  
  12.             // no delay for most  
  13. }  
  14.   
  15. public int getRetryDelaySeconds() {  
  16.     return (Integer) kp.get("retryDelaySeconds");  
  17. }  

 

由于heritrix 默认是等待900秒, 也就是15分钟, 如果抓取失败一个小时也只能运行4次, 8 个小时也就是32次, 难怪一直在运行啊

 

Java代码   收藏代码
  1. /** for retryable problems, seconds to wait before a retry */  
  2.   {  
  3.       setRetryDelaySeconds(900);  
  4.   }  

 知道原因后就好办了, 修改一下配置文件:

 

Xml代码   收藏代码
  1. <!-- FRONTIER: Record of all URIs discovered and queued-for-collection -->  
  2. <bean id="frontier"   
  3.   class="org.archive.crawler.frontier.BdbFrontier">  
  4.  <!-- <property name="holdQueues" value="true" /> -->  
  5.  <!-- <property name="queueTotalBudget" value="-1" /> -->  
  6.  <!-- <property name="balanceReplenishAmount" value="3000" /> -->  
  7.  <!-- <property name="errorPenaltyAmount" value="100" /> -->  
  8.  <!-- <property name="precedenceFloor" value="255" /> -->  
  9.  <!-- <property name="queuePrecedencePolicy">  
  10.        <bean class="org.archive.crawler.frontier.precedence.BaseQueuePrecedencePolicy" />  
  11.       </property> -->  
  12.  <!-- <property name="snoozeLongMs" value="300000" /> -->  
  13.   <property name="retryDelaySeconds" value="90" />   
  14.  <!-- <property name="maxRetries" value="30" /> -->  
  15.  <!-- <property name="recoveryDir" value="logs" /> -->  
  16.  <!-- <property name="recoveryLogEnabled" value="true" /> -->  
  17.  <!-- <property name="maxOutlinks" value="6000" /> -->  
  18.  <!-- <property name="outboundQueueCapacity" value="50" /> -->  
  19.  <!-- <property name="inboundQueueMultiple" value="3" /> -->  
  20.  <!-- <property name="dumpPendingAtClose" value="false" /> -->  
  21. </bean>  

 

这是heritrix3的配置, 把时间改成90秒, 也就是只等待1分半钟,

如果是H1的配置, 那可以用管理界面进行配置。

改了一下速度一下提高了很多, 原来8小时才能爬完一个网站, 现在2个小时就行了。

如果再用一下heritrix

增量抓取, 那下次再抓取这个网站时, 速度又会增加很多。这样问题解决了

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值