zoukankan      html  css  js  c++  java
  • 爬虫程序判断是否已抓URL

    http://www.cnblogs.com/yuandong/archive/2008/08/28/Web_Spider_Url_Index.html

    看了这篇文章后的灵感,不过他是用C++实现的,我是用C#实现。。不说那么多,帖代码了。。

    判断URLIdentity类,用于对已抓取的URL进行标识,判断URL是否已抓取

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Security.Cryptography;

    namespace TestMD5
    {
        
    public class URLIdentity
        {
            
    private BitArray []SegmentArray = new BitArray[4096];

            
    private int GetSegmentIndex(int hashValue)
            {
                
    return hashValue / 25000;
            }

            
    private int GetSegmentOffset(int hashValue)
            {
                
    return hashValue % 25000;
            }

            
    public int GetIntHashCode(string url)
            {
                
    byte[] tmpByte;
                MD5 md5 
    = new MD5CryptoServiceProvider();
                tmpByte 
    = md5.ComputeHash(Encoding.Default.GetBytes(url));
                StringBuilder sb 
    = new StringBuilder();
                
    for (int i = 0; i < tmpByte.Length; i++)
                {
                    sb.Append(tmpByte[i]);
                }
                
    string temp = sb.ToString().Substring(08); 
                
    return Int32.Parse(temp);
            }

            
    public void SetUrlIndentity(string url)
            {
                
    int HashNum = GetIntHashCode(url);
                
    int SegIndex = GetSegmentIndex(HashNum);
                
    int SegOffset = GetSegmentOffset(HashNum);

                
    if (SegmentArray[SegIndex] == null)
                {
                    SegmentArray[SegIndex] 
    = new BitArray(25000);
                }

                SegmentArray[SegIndex][SegOffset] 
    = true;
            }

            
    public bool GetUrlIdentity(string url)
            {
                
    int HashNum = GetIntHashCode(url);
                
    int SegIndex = GetSegmentIndex(HashNum);
                
    int SegOffset = GetSegmentOffset(HashNum);

                
    if (SegmentArray[SegIndex] == null)
                {
                    
    return false;
                }
                
    else
                {
                    
    return SegmentArray[SegIndex][SegOffset];
                }
            }
        }
    }

    DEMO:

    Code
  • 相关阅读:
    jmeter的插件安装
    linux下性能监控工具nmon的使用
    kafka如何保证不重复消费又不丢失数据_Kafka写入的数据如何保证不丢失?
    Goroutine和Panic
    go 并发有趣现象和要避开的坑
    Go语言宕机恢复(recover)——防止程序崩溃
    invalid character 'è' looking for beginning of value
    golang实现RPC的几种方式
    channl与select
    我要在栈上。不,你应该在堆上
  • 原文地址:https://www.cnblogs.com/coolkiss/p/1517460.html
Copyright © 2011-2022 走看看