上次已經可以得到頁面的HTML代碼了,接下來需要對HTML代碼分析,得到里面所有的鏈接和過濾掉沒用的HTML代碼,把文字內容保留下來。
分析HTML代碼,通過正規表達式將鏈接和鏈接的文字內容保存下來。
private void FindLink(string html) { this.TextBox3.Text=""; List<string> hrefList = new List<string>();//鏈接 List<string> nameList = new List<string>();//鏈接名稱 string pattern = @"<a\s*href=(""|')(?<href>[\s\S.]*?)(""|').*?>\s*(?<name>[\s\S.]*?)</a>"; MatchCollection mc = Regex.Matches(html, pattern); foreach (Match m in mc) { if (m.Success) { //加入集合數組 hrefList.Add(m.Groups["href"].Value); nameList.Add(m.Groups["name"].Value); this.TextBox3.Text += m.Groups["href"].Value + "|" + m.Groups["name"].Value + "\n"; } } }
這個方法只實現簡單的找到鏈接,並沒有過濾掉#或javascript:void(0)這樣的內容。
接下要過濾掉沒有用的HTML代碼,保留文字內容,基本還是正規表達式,網上還有很多種方法,寫的正規的HTML頁面都可以正常過濾掉,不過對於那些代碼都不成對的、不按常理出牌的網站,我就很無語了……
public string ClearHtml(string text)//過濾html,js,css代碼 { text = text.Trim(); if (string.IsNullOrEmpty(text)) return string.Empty; text = Regex.Replace(text, "<head[^>]*>(?:.|[\r\n])*?</head>", ""); text = Regex.Replace(text, "<script[^>]*>(?:.|[\r\n])*?</script>", ""); text = Regex.Replace(text, "<style[^>]*>(?:.|[\r\n])*?</style>", ""); text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", ""); //<br> text = Regex.Replace(text, "\\&[a-zA-Z]{1,10};", ""); text = Regex.Replace(text, "<[^>]*>", ""); text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", ""); // text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //其它任何標記 text = Regex.Replace(text, "[\\s]{2,}", " "); //兩個或多個空格替換為一個 text = text.Replace("'", "''"); text = text.Replace("\r\n", ""); text = text.Replace(" ", ""); text = text.Replace("\t", ""); return text.Trim(); }
最后再加個通過URL分析IP地址的方法,有些域名做均衡負載的都可以分析出多個IP,不過只能在本地運行,放IIS上需要完整的信任級別,關於信任級別的說明請點這里。
private void IPAddresses(string url) { url = url.Substring(url.IndexOf("//") + 2); if (url.IndexOf("/") != -1) { url = url.Remove(url.IndexOf("/")); } this.Literal1.Text += "<br>" + url; try { System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding(); IPHostEntry ipHostEntry = Dns.GetHostEntry(url); System.Net.IPAddress[] ipaddress = ipHostEntry.AddressList; foreach (IPAddress item in ipaddress) { this.Literal1.Text += "<br>IP:" + item; } } catch { } }