专栏名称: 程序员大咖
为程序员提供最优质的博文、最精彩的讨论、最实用的开发资源;提供最新最全的编程学习资料:PHP、Objective-C、Java、Swift、C/C++函数库、.NET Framework类库、J2SE API等等。并不定期奉送各种福利。
目录
相关文章推荐
程序猿  ·  Python有史以来最强大的挑战者终于出现 ·  昨天  
极客之家  ·  视频一键转图文,这款开源的 AI ... ·  2 天前  
京东科技技术说  ·  JDK从8升级到21的问题集 ·  2 天前  
51好读  ›  专栏  ›  程序员大咖

Python 编写知乎爬虫实践

程序员大咖  · 公众号  · 程序员  · 2018-04-20 10:24

正文

请到「今天看啥」查看全文


5000000

  • class BloomFilter :

  • def __init__ ( self ):

  • # Initialize bloom filter, set size and all bits to 0

  • bit_array = bitarray ( BIT_SIZE )

  • bit_array . setall ( 0 )

  • self . bit_array = bit_array

  • def add ( self , url ):

  • # Add a url, and set points in bitarray to 1 (Points count is equal to hash funcs count.)

  • # Here use 7 hash functions.

  • point_list = self . get_postions ( url )

  • for b in point_list :

  • self . bit_array [ b ] = 1

  • def contains ( self , url ):

  • # Check if a url is in a collection

  • point_list = self . get_postions ( url )

  • result = True

  • for b in point_list :

  • result = result and self . bit_array [ b ]

  • return result

  • def get_postions ( self , url ):

  • # Get points positions in bit vector.

  • point1 = mmh3 . hash ( url , 41 ) % BIT_SIZE

  • point2 = mmh3 . hash ( url , 42 ) % BIT_SIZE

  • point3 = mmh3 . hash ( url , 43 ) % BIT_SIZE

  • point4 = mmh3 . hash ( url , 44 ) %







  • 请到「今天看啥」查看全文