在网上无意看到这个东西,我大概看了一下C#的代码,里面实现很简单,当然只能算是一个演示版本吧!不可能有Google Baidu的那么强大。有爬虫代码 Web代码
主要的代码分析如下:
/// <summary>
/// 取得该URL网页内容数据.调用后面的分析函数。
/// </summary>
private void GetHTML() {
if (_HTMLData != null) return;
Byte[] BinData;
try {
WebClient wc = new WebClient();
BinData = wc.DownloadData(_URL);
_HTMLData = Encoding.Default.GetString(BinData);
wc.Dispose();
}
catch (Exception) { Console.WriteLine("Can not read this page!"); return; }
GetTitle();
GetMeta();
GetLink();
if (OnFinishAnalyze != null) { OnFinishAnalyze(this, new EventArgs()); }
Console.WriteLine("Finish!");
this.Dispose();
}
/// <summary>
/// 分析URL里的Title
/// </summary>
private void GetTitle() {
if (_Title != null) return;
//"(<title>([\S\s]+)</title>)" 提取网页标题的正则表达式
Regex reg = new Regex(@"<title>([\S\s]+)</title>", RegexOptions.IgnoreCase);
try{
Match m = reg.Matches(_HTMLData)[0];
if (m.Success) _Title = m.Groups[1].Captures[0].ToString();
}
catch (Exception) { _Title = ""; }
}
/// <summary>
/// 解析Html代码里的超链接.获得子URL集.
/// </summary>
private void GetLink() {
if (_ChildURLSet != null) return;
ArrayList urlset=new ArrayList();
//"<a[\\s]+href=\"?([\\S]+)\"?[^ <>]+>([^ <>]+)</a>" //提取超链接的正则表达式
Regex reg = new Regex("<a[\\s]+href=\"?([\\S]+)\"?[^<>]+>([^<>]+?)</a>", RegexOptions.IgnoreCase);
MatchCollection mm;