爬虫程序判断是否已抓URL
2009-07-06 09:53
127 查看
/article/4803483.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(0, 8);
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
using System.Runtime.InteropServices;
using System.Collections;
namespace TestMD5
{
class Program
{
static void Main(string[] args)
{
URLIdentity urlIdentity = new URLIdentity();
string str = "http://www.cnblogs.com";
urlIdentity.SetUrlIndentity(str)
if(urlIdentity.GetUrlIdentity(str))
{
Console.WriteLine("this url had been crawled");
}
}
}
}
看了这篇文章后的灵感,不过他是用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(0, 8);
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
using System.Runtime.InteropServices;
using System.Collections;
namespace TestMD5
{
class Program
{
static void Main(string[] args)
{
URLIdentity urlIdentity = new URLIdentity();
string str = "http://www.cnblogs.com";
urlIdentity.SetUrlIndentity(str)
if(urlIdentity.GetUrlIdentity(str))
{
Console.WriteLine("this url had been crawled");
}
}
}
}
相关文章推荐
- 论判断一个 URL 是否已经被爬虫处理
- 判断输入的字符串是否为合法IP地址的程序
- .net 判断程序是否运行在虚拟机上(方法1)
- 判断程序是否运行在虚拟机里 收藏
- 版本新特性(在程序启动的时候判断是否是新版本)
- 完成系统登录程序,从命令行输入用户名和密码,如果没有输入用户名和密码,则提示输入用户名和密码;如果输入了用户名但是没有输入密码,则提示用户输入密码,然后判断用户名是否是mldn,密码是否是hello,
- 完成系统登录程序,从命令行输入用户名和密码,如果没有输入用户名和密码,则提示输入用户名和密码;如果输入了用户名但是没有输入密码,则提示用户输入密码,然后判断用户名是否是mldn,密码是否是hello,
- 判断指定的进程或程序是否存在方法
- Java小程序之1000000数字判断是否为回数
- 使用android SharedPreference判断程序是否第一次运行
- c#读取进程列表判断程序是否已经启动(转)
- .net中判断该应用程序是否已经启动,防止重复启动,监控程序启动是否正常
- C118 smsweb: 通过日志关键字检测判断obb程序是否工作正常_20160617_七侠镇莫尛貝
- 怎么判断一个ASP.NET的程序是否三层架构
- 判断IP是否为爬虫IP
- Java小程序之判断输入年份是否是闰年
- 判断是否URL网址(PHP代码/函数)
- Mac OS X开发中在程序运行时判断某个API是否存在
- InnoSetup 安装和卸载时判断程序是否运行的正确方式
- zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来。 规定,所有的边都只能画一次,不能重复画。 输入 第一行只有一个正整数N(N<=