蜘蛛3分钟找到了love

我想知道:一台PC上运行的蜘蛛,从sina.com作为起始url, 最快花多少时间搜索到love这个词?
下面是python写的简单的宽度搜索的蜘蛛
  1. # -*- coding: UTF-8 -*-
  2. ''''' given a key and start_url, the spider is suppose to find the key in html as fast as possible. 
  3.  of course this program is not fast, just a bottom line test. 
  4. '''
  5. import Queue
  6. import spider_lib as slib
  7. import re

  8. if __name__ == "__main__":
  9.     start_url = "http://www.sina.com"
  10.     key = r'/blove/b'
  11.     urls = Queue.Queue()
  12.     url_hash = {} 
  13.     urls.put( start_url )
  14.     cnt = 0
  15.     while 1:
  16.         url = urls.get()
  17.         if not url_hash.has_key( url ):
  18.             try:
  19.                 print 'process ' + url 
  20.                 lines = slib.fetch_page_as_lines( url ) 
  21.             except Exception, e:
  22.                 print e
  23.             match_line = slib.extract_lines( lines, key )
  24.             if match_line != []:
  25.                 print 'got %s in %s at %s' % ( key, match_line[0][0], url )
  26.                 break
  27.             url_hash[ url ] = 1
  28.             domain_name = url.split("/"3 )[2
  29.             for line in lines:
  30.                 for new_url in re.findall( r'<a[^>]*href="([^"]*)"', line ):
  31.                     if new_url.lower().startswith( 'http' ):
  32.                         urls.put( new_url ) 
  33.                     elif not re.match( '[^/]+://', new_url ):
  34.                         urls.put( "http://" + domain_name + '/' + new_url )
  35.                         
  36.         
然后在shell下运行 >> time python ./search.py
结果是接近6分钟: 
    real    2m57.054s
    user    0m0.332s
     sys    0m0.048s

从程序角度看确实相当慢,不过仅仅作为一种‘底线’程序,存在许多优化方案:
多线程
用C改写
应聘到sina然后在他们服务器上面运行
。。。

你想试试吗?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值