程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成抓取和剖析網頁類實例

C#完成抓取和剖析網頁類實例

編輯:C#入門知識

C#完成抓取和剖析網頁類實例。本站提示廣大學習愛好者:(C#完成抓取和剖析網頁類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成抓取和剖析網頁類實例正文


本文實例講述了C#完成抓取和剖析網頁類。分享給年夜家供年夜家參考。詳細剖析以下:

這裡引見了抓取和剖析網頁的類。

其重要功效有:

1、提取網頁的純文本,去一切html標簽和javascript代碼
2、提取網頁的鏈接,包含href和frame及iframe
3、提取網頁的title等(其它的標簽可依此類推,正則是一樣的)
4、可以完成簡略的表單提交及cookie保留

/*
* Author:Sunjoy at CCNU
* 假如您改良了這個類請發一份代碼給我(ccnusjy 在gmail.com)
*/
using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
/// <summary>
/// 網頁類
/// </summary>
public class WebPage
{
 #region 公有成員
 private Uri m_uri; //網址
 private List<Link> m_links; //此網頁上的鏈接
 private string m_title;  //此網頁的題目
 private string m_html;   //此網頁的HTML代碼
 private string m_outstr;  //此網頁可輸入的純文本
 private bool m_good;   //此網頁能否可用
 private int m_pagesize;  //此網頁的年夜小
 private static Dictionary<string, CookieContainer> webcookies = new Dictionary<string, CookieContainer>();//寄存一切網頁的Cookie
 private string m_post; //此網頁的上岸頁須要的POST數據
 private string m_loginurl; //此網頁的上岸頁
 #endregion
 #region 公有辦法
 /// <summary>
 /// 這公有辦法從網頁的HTML代碼平分析出鏈接信息
 /// </summary>
 /// <returns>List<Link></returns>
 private List<Link> getLinks()
 {
  if (m_links.Count == 0)
  {
   Regex[] regex = new Regex[2];
   regex[0] = new Regex("(?m)<a[^><]+href=(\"|')?(?<url>([^>\"'\\s)])+)(\"|')?[^>]*>(?<text>(\\w|\\W)*?)</", RegexOptions.Multiline | RegexOptions.IgnoreCase);
   regex[1] = new Regex("<[i]*frame[^><]+src=(\"|')?(?<url>([^>\"'\\s)])+)(\"|')?[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
   for (int i = 0; i < 2; i++)
   {
    Match match = regex[i].Match(m_html);
    while (match.Success)
    {
     try
     {
      string url = new Uri(m_uri, match.Groups["url"].Value).AbsoluteUri;
      string text = "";
      if (i == 0) text = new Regex("(<[^>]+>)|(\\s)|( )|&|\"", RegexOptions.Multiline | RegexOptions.IgnoreCase).WordStr(match.Groups["text"].Value, "");
      Link link = new Link(url, text);
      m_links.Add(link);
     }
     catch(Exception ex){Console.WriteLine(ex.Message); };
     match = match.NextMatch();
    }
   }
  }
  return m_links;
 }
 /// <summary>
 /// 此公有辦法從一段HTML文本中提掏出必定字數的純文本
 /// </summary>
 /// <param name="instr">HTML代碼</param>
 /// <param name="firstN">提取從頭數若干個字</param>
 /// <param name="withLink">能否要鏈接外面的字</param>
 /// <returns>純文本</returns>
 private string getFirstNchar(string instr, int firstN, bool withLink)
 {
  if (m_outstr == "")
  {
   m_outstr = instr.Clone() as string;
   m_outstr = new Regex(@"(?m)<script[^>]*>(\w|\W)*?</script[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase ).WordStr(m_outstr, "");
   m_outstr = new Regex(@"(?m)<style[^>]*>(\w|\W)*?</style[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase ).WordStr(m_outstr, "");
   m_outstr = new Regex(@"(?m)<select[^>]*>(\w|\W)*?</select[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase ).WordStr(m_outstr, "");
   if (!withLink) m_outstr = new Regex(@"(?m)<a[^>]*>(\w|\W)*?</a[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).WordStr(m_outstr, "");
   Regex objReg = new System.Text.RegularExpressions.Regex("(<[^>]+?>)| ", RegexOptions.Multiline | RegexOptions.IgnoreCase);
   m_outstr = objReg.WordStr(m_outstr, "");
   Regex objReg2 = new System.Text.RegularExpressions.Regex("(\\s)+", RegexOptions.Multiline | RegexOptions.IgnoreCase);
   m_outstr = objReg2.WordStr(m_outstr, " ");
  }
  return m_outstr.Length > firstN ? m_outstr.Substring(0, firstN) : m_outstr;
 }
 /// <summary>
 /// 此公有辦法前往一個IP地址對應的無符號整數
 /// </summary>
 /// <param name="x">IP地址</param>
 /// <returns></returns>
 private uint getuintFromIP(IPAddress x)
 {
  Byte[] bt = x.GetAddressBytes();
  uint i = (uint)(bt[0] * 256 * 256 * 256);
  i += (uint)(bt[1] * 256 * 256);
  i += (uint)(bt[2] * 256);
  i += (uint)(bt[3]);
  return i;
 }
 #endregion
 #region 私有文法
 /// <summary>
 /// 此私有辦法提取網頁中必定字數的純文本,包含鏈接文字
 /// </summary>
 /// <param name="firstN">字數</param>
 /// <returns></returns>
 public string getContext(int firstN)
 {
  return getFirstNchar(m_html, firstN, true);
 }
 /// <summary>
 /// 此私有辦法提取網頁中必定字數的純文本,不包含鏈接文字
 /// </summary>
 /// <param name="firstN"></param>
 /// <returns></returns>
 public string getContextWithOutLink(int firstN)
 {
  return getFirstNchar(m_html, firstN, false);
 }
 /// <summary>
 /// 此私有辦法從本網頁的鏈接中提取必定數目的鏈接,該鏈接的URL知足某正則式
 /// </summary>
 /// <param name="pattern">正則式</param>
 /// <param name="count">前往的鏈接的個數</param>
 /// <returns>List<Link></returns>
 public List<Link> getSpecialLinksByUrl(string pattern,int count)
 {
  if(m_links.Count==0)getLinks();
  List<Link> SpecialLinks = new List<Link>();
  List<Link>.Enumerator i;
  i = m_links.GetEnumerator();
  int cnt = 0;
  while (i.MoveNext() && cnt<count)
  {
   if (new Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Match(i.Current.url).Success)
   {
    SpecialLinks.Add(i.Current);
    cnt++;
   }
  } 
  return SpecialLinks;
 }
 /// <summary>
 /// 此私有辦法從本網頁的鏈接中提取必定數目的鏈接,該鏈接的文字知足某正則式
 /// </summary>
 /// <param name="pattern">正則式</param>
 /// <param name="count">前往的鏈接的個數</param>
 /// <returns>List<Link></returns>
 public List<Link> getSpecialLinksByText(string pattern,int count)
 {
  if (m_links.Count == 0) getLinks();
  List<Link> SpecialLinks = new List<Link>();
  List<Link>.Enumerator i;
  i = m_links.GetEnumerator();
  int cnt = 0;
  while (i.MoveNext() && cnt < count)
  {
   if (new Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Match(i.Current.text).Success)
   {
    SpecialLinks.Add(i.Current);
    cnt++;
   }
  }
  return SpecialLinks;
 }
 /// <summary>
 /// 此私有辦法取得一切鏈接中在必定IP規模的鏈接
 /// </summary>
 /// <param name="_ip_start">肇端IP</param>
 /// <param name="_ip_end">終止IP</param>
 /// <returns></returns>
 public List<Link> getSpecialLinksByIP(string _ip_start, string _ip_end)
 {
  IPAddress ip_start = IPAddress.Parse(_ip_start);
  IPAddress ip_end = IPAddress.Parse(_ip_end);
  if (m_links.Count == 0) getLinks();
  List<Link> SpecialLinks = new List<Link>();
  List<Link>.Enumerator i;
  i = m_links.GetEnumerator();
  while (i.MoveNext())
  {
   IPAddress ip;
   try
   {
    ip = Dns.GetHostEntry(new Uri(i.Current.url).Host).AddressList[0];
   }
   catch { continue; }
   if(getuintFromIP(ip)>=getuintFromIP(ip_start) && getuintFromIP(ip)<=getuintFromIP(ip_end))
   {
    SpecialLinks.Add(i.Current);
   }
  }
  return SpecialLinks;
 }
 /// <summary>
 /// 這私有辦法提取本網頁的純文本中知足某正則式的文字
 /// </summary>
 /// <param name="pattern">正則式</param>
 /// <returns>前往文字</returns>
 public string getSpecialWords(string pattern)
 {
  if (m_outstr == "") getContext(Int16.MaxValue);
  Regex regex = new Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase );
  Match mc=regex.Match(m_outstr);
  if (mc.Success)
   return mc.Groups[1].Value;
  return string.Empty;
 }
 #endregion
 #region 結構函數
 private void Init(string _url)
 {
  try
  {
   m_uri = new Uri(_url);
   m_links = new List<Link>();
   m_html = "";
   m_outstr = "";
   m_title = "";
   m_good = true;
   if (_url.EndsWith(".rar") || _url.EndsWith(".dat") || _url.EndsWith(".msi"))
   {
    m_good = false;
    return;
   }
   HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(m_uri);
   rqst.AllowAutoRedirect = true;
   rqst.MaximumAutomaticRedirections = 3;
   rqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
   rqst.KeepAlive = true;
   rqst.Timeout = 30000;
   lock (WebPage.webcookies)
   {
    if (WebPage.webcookies.ContainsKey(m_uri.Host))
     rqst.CookieContainer = WebPage.webcookies[m_uri.Host];
    else
    {
     CookieContainer cc = new CookieContainer();
     WebPage.webcookies[m_uri.Host] = cc;
     rqst.CookieContainer = cc;
    }
   }
   HttpWebResponse rsps = (HttpWebResponse)rqst.GetResponse();
   Stream sm = rsps.GetResponseStream();
   if (!rsps.ContentType.ToLower().StartsWith("text/") || rsps.ContentLength > 1 << 22)
   {
    rsps.Close();
    m_good = false;
    return;
   }
   Encoding cding = System.Text.Encoding.Default;
   string contenttype=rsps.ContentType.ToLower();
   int ix = contenttype.IndexOf("charset=");
   if (ix != -1)
   {
    try
    {
     cding = System.Text.Encoding.GetEncoding(rsps.ContentType.Substring(ix + "charset".Length + 1));
    }
    catch
    {
     cding = Encoding.Default;
    }
    m_html = new StreamReader(sm, cding).ReadToEnd();
   }
   else
   {
    m_html = new StreamReader(sm, cding).ReadToEnd();
    Regex regex = new Regex("charset=(?<cding>[^=]+)?\"",RegexOptions.IgnoreCase);
    string strcding = regex.Match(m_html).Groups["cding"].Value;
    try
    {
     cding = Encoding.GetEncoding(strcding);
    }
    catch{
     cding = Encoding.Default;
    }
    byte[] bytes=Encoding.Default.GetBytes(m_html.ToCharArray());
    m_html = cding.GetString(bytes);
    if (m_html.Split('?').Length > 100)
    {
     m_html=Encoding.Default.GetString(bytes);
    }
   }
   
   m_pagesize = m_html.Length;
   m_uri = rsps.ResponseUri;
   rsps.Close();
  }
  catch (Exception ex)
  {
   Console.WriteLine(ex.Message+m_uri.ToString());
   m_good = false;
  }
 }
 public WebPage(string _url)
 {
  string uurl = "";
  try
  {
   uurl = Uri.UnescapeDataString(_url);
   _url = uurl;
  }
  catch { };
  Regex re = new Regex("(?<h>[^\x00-\xff]+)");
  Match mc = re.Match(_url);
  if (mc.Success)
  {
   string han = mc.Groups["h"].Value;
   _url = _url.WordStr(han, System.Web.HttpUtility.UrlEncode(han, Encoding.GetEncoding("GB2312")));
  }
  Init(_url);
 }
 public WebPage(string _url, string _loginurl, string _post)
 {
  string uurl = "";
  try
  {
   uurl = Uri.UnescapeDataString(_url);
   _url = uurl;
  }
  catch { };
  Regex re = new Regex("(?<h>[^\x00-\xff]+)");
  Match mc = re.Match(_url);
  if (mc.Success)
  {
   string han = mc.Groups["h"].Value;
   _url = _url.WordStr(han, System.Web.HttpUtility.UrlEncode(han, Encoding.GetEncoding("GB2312")));
  }
  if (_loginurl.Trim() == "" || _post.Trim() == "" || WebPage.webcookies.ContainsKey(new Uri(_url).Host))
  {
   Init(_url);
  }
  else
  {
   #region 上岸
   string indata = _post;
   m_post = _post;
   m_loginurl = _loginurl;
   byte[] bytes = Encoding.Default.GetBytes(_post);
   CookieContainer myCookieContainer = new CookieContainer();
   try
   {
    //新建一個CookieContainer來寄存Cookie聚集 
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(_loginurl);
    //新建一個HttpWebRequest 
    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
    myHttpWebRequest.AllowAutoRedirect = false;
    myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    myHttpWebRequest.Timeout = 60000;
    myHttpWebRequest.KeepAlive = true;
    myHttpWebRequest.ContentLength = bytes.Length;
    myHttpWebRequest.Method = "POST";
    myHttpWebRequest.CookieContainer = myCookieContainer;
    //設置HttpWebRequest的CookieContainer為適才樹立的誰人myCookieContainer 
    Stream myRequestStream = myHttpWebRequest.GetRequestStream();
    myRequestStream.Write(bytes, 0, bytes.Length);
    myRequestStream.Close();
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    foreach (Cookie ck in myHttpWebResponse.Cookies)
    {
     myCookieContainer.Add(ck);
    }
    myHttpWebResponse.Close();
   }
   catch
   {
    Init(_url);
    return;
   }
   #endregion
   #region 上岸後再拜訪頁面
   try
   {
    m_uri = new Uri(_url);
    m_links = new List<Link>();
    m_html = "";
    m_outstr = "";
    m_title = "";
    m_good = true;
    if (_url.EndsWith(".rar") || _url.EndsWith(".dat") || _url.EndsWith(".msi"))
    {
     m_good = false;
     return;
    }
    HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(m_uri);
    rqst.AllowAutoRedirect = true;
    rqst.MaximumAutomaticRedirections = 3;
    rqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    rqst.KeepAlive = true;
    rqst.Timeout = 30000;
    rqst.CookieContainer = myCookieContainer;
    lock (WebPage.webcookies)
    {
     WebPage.webcookies[m_uri.Host] = myCookieContainer;
    }
    HttpWebResponse rsps = (HttpWebResponse)rqst.GetResponse();
    Stream sm = rsps.GetResponseStream();
    if (!rsps.ContentType.ToLower().StartsWith("text/") || rsps.ContentLength > 1 << 22)
    {
     rsps.Close();
     m_good = false;
     return;
    }
    Encoding cding = System.Text.Encoding.Default;
    int ix = rsps.ContentType.ToLower().IndexOf("charset=");
    if (ix != -1)
    {
     try
     {
      cding = System.Text.Encoding.GetEncoding(rsps.ContentType.Substring(ix + "charset".Length + 1));
     }
     catch
     {
      cding = Encoding.Default;
     }
    }
    m_html = new StreamReader(sm, cding).ReadToEnd();
    m_pagesize = m_html.Length;
    m_uri = rsps.ResponseUri;
    rsps.Close();
   }
   catch (Exception ex)
   {
    Console.WriteLine(ex.Message+m_uri.ToString());
    m_good = false;
   }
   #endregion
  }
 }
 #endregion
 #region 屬性
 /// <summary>
 /// 經由過程此屬性可取得本網頁的網址,只讀
 /// </summary>
 public string URL
 {
  get
  {
   return m_uri.AbsoluteUri;
  }
 }
 /// <summary>
 /// 經由過程此屬性可取得本網頁的題目,只讀
 /// </summary>
 public string Title
 {
  get
  {
   if (m_title == "")
   {
    Regex reg = new Regex(@"(?m)<title[^>]*>(?<title>(?:\w|\W)*?)</title[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase );
    Match mc = reg.Match(m_html);
    if (mc.Success)
     m_title= mc.Groups["title"].Value.Trim();
   }
   return m_title;
  }
 }
 /// <summary>
 /// 此屬性取得本網頁的一切鏈接信息,只讀
 /// </summary>
 public List<Link> Links
 {
  get
  {
   if (m_links.Count == 0) getLinks();
   return m_links;
  }
 }
 /// <summary>
 /// 此屬性前往本網頁的全體純文本信息,只讀
 /// </summary>
 public string Context
 {
  get
  {
   if (m_outstr == "") getContext(Int16.MaxValue);
   return m_outstr;
  }
 }
 /// <summary>
 /// 此屬性取得本網頁的年夜小
 /// </summary>
 public int PageSize
 {
  get
  {
   return m_pagesize;
  }
 }
 /// <summary>
 /// 此屬性取得本網頁的一切站內鏈接
 /// </summary>
 public List<Link> InsiteLinks
 {
  get
  {
   return getSpecialLinksByUrl("^http://"+m_uri.Host,Int16.MaxValue);
  }
 }
 /// <summary>
 /// 此屬性表現本網頁能否可用
 /// </summary>
 public bool IsGood
 {
  get
  {
   return m_good;
  }
 }
 /// <summary>
 /// 此屬性表現網頁的地點的網站
 /// </summary>
 public string Host
 {
  get
  {
   return m_uri.Host;
  }
 }
 /// <summary>
 /// 此網頁的上岸頁所需的POST數據
 /// </summary>
 public string PostStr
 {
  get
  {
   return m_post;
  }
 }
 /// <summary>
 /// 此網頁的上岸頁
 /// </summary>
 public string LoginURL
 {
  get
  {
   return m_loginurl;
  }
 }
 #endregion
}
/// <summary>
/// 鏈接類
/// </summary>
public class Link
{
 public string url; //鏈接網址
 public string text; //鏈接文字
 public Link(string _url, string _text)
 {
  url = _url;
  text = _text;
 }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved