打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
抓取和分析网页的类源代码
抓取和分析网页的类源代码

抓取和分析网页的类。

主要功能有:
1、提取网页的纯文本,去所有html标签和javascript代码
2、提取网页的链接,包括href和frame及iframe
3、提取网页的title等(其它的标签可依此类推,正则是一样的)
4、可以实现简单的表单提交及cookie保存

复制
 
保存
  1. <STRONG class=c>/*  
  2. *  Author:Sunjoy at CCNU  
  3. *  如果您改进了这个类请发一份代码给我(ccnusjy 在gmail.com)  
  4. */</STRONG>   
  5.   
  6. <STRONG class=k>using</STRONG> System;   
  7. <STRONG class=k>using</STRONG> System.Data;   
  8. <STRONG class=k>using</STRONG> System.Configuration;   
  9. <STRONG class=k>using</STRONG> System.Net;   
  10. <STRONG class=k>using</STRONG> System.IO;   
  11. <STRONG class=k>using</STRONG> System.Text;   
  12. <STRONG class=k>using</STRONG> System.Collections.Generic;   
  13. <STRONG class=k>using</STRONG> System.Text.RegularExpressions;   
  14. <STRONG class=k>using</STRONG> System.Threading;   
  15. <STRONG class=k>using</STRONG> System.Web;   
  16. <STRONG class=c>/// <summary>   
  17. </STRONG><STRONG class=c>/// 网页类   
  18. </STRONG><STRONG class=c>/// </summary>   
  19. </STRONG><STRONG class=k>public</STRONG> <STRONG class=k>class</STRONG> WebPage   
  20. {   
  21. <STRONG class=r>  
  22.     #region 私有成员   
  23. </STRONG>    <STRONG class=k>private</STRONG> Uri m_uri;   <STRONG class=c>//网址   
  24. </STRONG>    <STRONG class=k>private</STRONG> List<Link> m_links;    <STRONG class=c>//此网页上的链接   
  25. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>string</STRONG> m_title;        <STRONG class=c>//此网页的标题   
  26. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>string</STRONG> m_html;         <STRONG class=c>//此网页的HTML代码   
  27. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>string</STRONG> m_outstr;       <STRONG class=c>//此网页可输出的纯文本   
  28. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>bool</STRONG> m_good;           <STRONG class=c>//此网页是否可用   
  29. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>int</STRONG> m_pagesize;       <STRONG class=c>//此网页的大小   
  30. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>static</STRONG> Dictionary<<STRONG class=k>string</STRONG>, CookieContainer> webcookies = <STRONG class=k>new</STRONG> Dictionary<<STRONG class=k>string</STRONG>, CookieContainer>();<STRONG class=c>//存放所有网页的Cookie   
  31. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>string</STRONG> m_post;  <STRONG class=c>//此网页的登陆页需要的POST数据   
  32. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>string</STRONG> m_loginurl;  <STRONG class=c>//此网页的登陆页   
  33. </STRONG><STRONG class=r>    #endregion   
  34. </STRONG><STRONG class=r>  
  35.  
  36.     #region 私有方法   
  37. </STRONG>    <STRONG class=c>/// <summary>   
  38. </STRONG>    <STRONG class=c>/// 这私有方法从网页的HTML代码中分析出链接信息   
  39. </STRONG>    <STRONG class=c>/// </summary>   
  40. </STRONG>    <STRONG class=c>/// <returns>List<Link></returns>   
  41. </STRONG>    <STRONG class=k>private</STRONG> List<Link> getLinks()   
  42.     {   
  43.         <STRONG class=k>if</STRONG> (m_links.Count == 0)   
  44.         {   
  45.             Regex[] regex = <STRONG class=k>new</STRONG> Regex[2];   
  46.             regex[0] = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>"(?m)<a[^><]+href=(\"|')?(?<url>([^>\"'\\s)])+)(\"|')?[^>]*>(?<text>(\\w|\\W)*?)</"</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase);   
  47.             regex[1] = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>"<[i]*frame[^><]+src=(\"|')?(?<url>([^>\"'\\s)])+)(\"|')?[^>]*>"</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase);   
  48.             <STRONG class=k>for</STRONG> (<STRONG class=k>int</STRONG> i = 0; i < 2; i++)   
  49.             {   
  50.                 Match match = regex[i].Match(m_html);   
  51.                 <STRONG class=k>while</STRONG> (match.Success)   
  52.                 {   
  53.                     <STRONG class=k>try</STRONG>   
  54.                     {   
  55.                         <STRONG class=k>string</STRONG> url = <STRONG class=k>new</STRONG> Uri(m_uri, match.Groups[<STRONG class=s>"url"</STRONG>].Value).AbsoluteUri;   
  56.                         <STRONG class=k>string</STRONG> text = <STRONG class=s>""</STRONG>;   
  57.                         <STRONG class=k>if</STRONG> (i == 0) text = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>"(<[^>]+>)|(\\s)|( )|&|\""</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(match.Groups[<STRONG class=s>"text"</STRONG>].Value, <STRONG class=s>""</STRONG>);   
  58.                         Link link = <STRONG class=k>new</STRONG> Link(url, text);   
  59.                         m_links.Add(link);   
  60.                     }   
  61.                     <STRONG class=k>catch</STRONG>(Exception ex){Console.WriteLine(ex.Message); };   
  62.                     match = match.NextMatch();   
  63.                 }   
  64.             }   
  65.         }   
  66.         <STRONG class=k>return</STRONG> m_links;   
  67.     }   
  68.       
  69.     <STRONG class=c>/// <summary>   
  70. </STRONG>    <STRONG class=c>/// 此私有方法从一段HTML文本中提取出一定字数的纯文本   
  71. </STRONG>    <STRONG class=c>/// </summary>   
  72. </STRONG>    <STRONG class=c>/// <param name="instr">HTML代码</param>   
  73. </STRONG>    <STRONG class=c>/// <param name="firstN">提取从头数多少个字</param>   
  74. </STRONG>    <STRONG class=c>/// <param name="withLink">是否要链接里面的字</param>   
  75. </STRONG>    <STRONG class=c>/// <returns>纯文本</returns>   
  76. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>string</STRONG> getFirstNchar(<STRONG class=k>string</STRONG> instr, <STRONG class=k>int</STRONG> firstN, <STRONG class=k>bool</STRONG> withLink)   
  77.     {   
  78.         <STRONG class=k>if</STRONG> (m_outstr == <STRONG class=s>""</STRONG>)   
  79.         {   
  80.             m_outstr = instr.Clone() <STRONG class=k>as</STRONG> <STRONG class=k>string</STRONG>;   
  81.             m_outstr = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>@"(?m)<script[^>]*>(\w|\W)*?</script[^>]*>"</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Replace(m_outstr, <STRONG class=s>""</STRONG>);   
  82.             m_outstr = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>@"(?m)<style[^>]*>(\w|\W)*?</style[^>]*>"</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Replace(m_outstr, <STRONG class=s>""</STRONG>);   
  83.             m_outstr = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>@"(?m)<select[^>]*>(\w|\W)*?</select[^>]*>"</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Replace(m_outstr, <STRONG class=s>""</STRONG>);   
  84.             <STRONG class=k>if</STRONG> (!withLink) m_outstr = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>@"(?m)<a[^>]*>(\w|\W)*?</a[^>]*>"</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, <STRONG class=s>""</STRONG>);   
  85.             Regex objReg = <STRONG class=k>new</STRONG> System.Text.RegularExpressions.Regex(<STRONG class=s>"(<[^>]+?>)| "</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase);   
  86.             m_outstr = objReg.Replace(m_outstr, <STRONG class=s>""</STRONG>);   
  87.             Regex objReg2 = <STRONG class=k>new</STRONG> System.Text.RegularExpressions.Regex(<STRONG class=s>"(\\s)+"</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase);   
  88.             m_outstr = objReg2.Replace(m_outstr, <STRONG class=s>" "</STRONG>);   
  89.         }   
  90.         <STRONG class=k>return</STRONG> m_outstr.Length > firstN ? m_outstr.Substring(0, firstN) : m_outstr;   
  91.     }   
  92.   
  93.     <STRONG class=c>/// <summary>   
  94. </STRONG>    <STRONG class=c>/// 此私有方法返回一个IP地址对应的无符号整数   
  95. </STRONG>    <STRONG class=c>/// </summary>   
  96. </STRONG>    <STRONG class=c>/// <param name="x">IP地址</param>   
  97. </STRONG>    <STRONG class=c>/// <returns></returns>   
  98. </STRONG>    <STRONG class=k>private</STRONG> <STRONG class=k>uint</STRONG> getuintFromIP(IPAddress x)   
  99.     {   
  100.         Byte[] bt = x.GetAddressBytes();   
  101.         <STRONG class=k>uint</STRONG> i = (<STRONG class=k>uint</STRONG>)(bt[0] * 256 * 256 * 256);   
  102.         i += (<STRONG class=k>uint</STRONG>)(bt[1] * 256 * 256);   
  103.         i += (<STRONG class=k>uint</STRONG>)(bt[2] * 256);   
  104.         i += (<STRONG class=k>uint</STRONG>)(bt[3]);   
  105.         <STRONG class=k>return</STRONG> i;   
  106.     }   
  107. <STRONG class=r>  
  108.     #endregion   
  109. </STRONG><STRONG class=r>  
  110.  
  111.     #region 公有文法   
  112. </STRONG>    <STRONG class=c>/// <summary>   
  113. </STRONG>    <STRONG class=c>/// 此公有方法提取网页中一定字数的纯文本,包括链接文字   
  114. </STRONG>    <STRONG class=c>/// </summary>   
  115. </STRONG>    <STRONG class=c>/// <param name="firstN">字数</param>   
  116. </STRONG>    <STRONG class=c>/// <returns></returns>   
  117. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> getContext(<STRONG class=k>int</STRONG> firstN)   
  118.     {   
  119.         <STRONG class=k>return</STRONG> getFirstNchar(m_html, firstN, <STRONG class=k>true</STRONG>);   
  120.     }   
  121.   
  122.     <STRONG class=c>/// <summary>   
  123. </STRONG>    <STRONG class=c>/// 此公有方法提取网页中一定字数的纯文本,不包括链接文字   
  124. </STRONG>    <STRONG class=c>/// </summary>   
  125. </STRONG>    <STRONG class=c>/// <param name="firstN"></param>   
  126. </STRONG>    <STRONG class=c>/// <returns></returns>   
  127. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> getContextWithOutLink(<STRONG class=k>int</STRONG> firstN)   
  128.     {   
  129.         <STRONG class=k>return</STRONG> getFirstNchar(m_html, firstN, <STRONG class=k>false</STRONG>);   
  130.     }   
  131.   
  132.     <STRONG class=c>/// <summary>   
  133. </STRONG>    <STRONG class=c>/// 此公有方法从本网页的链接中提取一定数量的链接,该链接的URL满足某正则式   
  134. </STRONG>    <STRONG class=c>/// </summary>   
  135. </STRONG>    <STRONG class=c>/// <param name="pattern">正则式</param>   
  136. </STRONG>    <STRONG class=c>/// <param name="count">返回的链接的个数</param>   
  137. </STRONG>    <STRONG class=c>/// <returns>List<Link></returns>   
  138. </STRONG>    <STRONG class=k>public</STRONG> List<Link> getSpecialLinksByUrl(<STRONG class=k>string</STRONG> pattern,<STRONG class=k>int</STRONG> count)   
  139.     {   
  140.         <STRONG class=k>if</STRONG>(m_links.Count==0)getLinks();   
  141.         List<Link> SpecialLinks = <STRONG class=k>new</STRONG> List<Link>();   
  142.         List<Link>.Enumerator i;   
  143.         i = m_links.GetEnumerator();   
  144.         <STRONG class=k>int</STRONG> cnt = 0;   
  145.         <STRONG class=k>while</STRONG> (i.MoveNext() && cnt<count)   
  146.         {   
  147.             <STRONG class=k>if</STRONG> (<STRONG class=k>new</STRONG> Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Match(i.Current.url).Success)   
  148.             {   
  149.                 SpecialLinks.Add(i.Current);   
  150.                 cnt++;   
  151.             }   
  152.         }    
  153.         <STRONG class=k>return</STRONG> SpecialLinks;   
  154.     }   
  155.   
  156.     
  157.   
  158.     <STRONG class=c>/// <summary>   
  159. </STRONG>    <STRONG class=c>/// 此公有方法从本网页的链接中提取一定数量的链接,该链接的文字满足某正则式   
  160. </STRONG>    <STRONG class=c>/// </summary>   
  161. </STRONG>    <STRONG class=c>/// <param name="pattern">正则式</param>   
  162. </STRONG>    <STRONG class=c>/// <param name="count">返回的链接的个数</param>   
  163. </STRONG>    <STRONG class=c>/// <returns>List<Link></returns>   
  164. </STRONG>    <STRONG class=k>public</STRONG> List<Link> getSpecialLinksByText(<STRONG class=k>string</STRONG> pattern,<STRONG class=k>int</STRONG> count)   
  165.     {   
  166.         <STRONG class=k>if</STRONG> (m_links.Count == 0) getLinks();   
  167.         List<Link> SpecialLinks = <STRONG class=k>new</STRONG> List<Link>();   
  168.         List<Link>.Enumerator i;   
  169.         i = m_links.GetEnumerator();   
  170.         <STRONG class=k>int</STRONG> cnt = 0;   
  171.         <STRONG class=k>while</STRONG> (i.MoveNext() && cnt < count)   
  172.         {   
  173.             <STRONG class=k>if</STRONG> (<STRONG class=k>new</STRONG> Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Match(i.Current.text).Success)   
  174.             {   
  175.                 SpecialLinks.Add(i.Current);   
  176.                 cnt++;   
  177.             }   
  178.         }   
  179.         <STRONG class=k>return</STRONG> SpecialLinks;   
  180.     }   
  181.     <STRONG class=c>/// <summary>   
  182. </STRONG>    <STRONG class=c>/// 此公有方法获得所有链接中在一定IP范围的链接   
  183. </STRONG>    <STRONG class=c>/// </summary>   
  184. </STRONG>    <STRONG class=c>/// <param name="_ip_start">起始IP</param>   
  185. </STRONG>    <STRONG class=c>/// <param name="_ip_end">终止IP</param>   
  186. </STRONG>    <STRONG class=c>/// <returns></returns>   
  187. </STRONG>    <STRONG class=k>public</STRONG> List<Link> getSpecialLinksByIP(<STRONG class=k>string</STRONG> _ip_start, <STRONG class=k>string</STRONG> _ip_end)   
  188.     {   
  189.         IPAddress ip_start = IPAddress.Parse(_ip_start);   
  190.         IPAddress ip_end = IPAddress.Parse(_ip_end);   
  191.         <STRONG class=k>if</STRONG> (m_links.Count == 0) getLinks();   
  192.         List<Link> SpecialLinks = <STRONG class=k>new</STRONG> List<Link>();   
  193.         List<Link>.Enumerator i;   
  194.         i = m_links.GetEnumerator();   
  195.         <STRONG class=k>while</STRONG> (i.MoveNext())   
  196.         {   
  197.             IPAddress ip;   
  198.             <STRONG class=k>try</STRONG>   
  199.             {   
  200.                 ip = Dns.GetHostEntry(<STRONG class=k>new</STRONG> Uri(i.Current.url).Host).AddressList[0];   
  201.             }   
  202.             <STRONG class=k>catch</STRONG> { <STRONG class=k>continue</STRONG>; }   
  203.             <STRONG class=k>if</STRONG>(getuintFromIP(ip)>=getuintFromIP(ip_start) && getuintFromIP(ip)<=getuintFromIP(ip_end))   
  204.             {   
  205.                 SpecialLinks.Add(i.Current);   
  206.             }   
  207.         }   
  208.         <STRONG class=k>return</STRONG> SpecialLinks;   
  209.     }   
  210.   
  211.     <STRONG class=c>/// <summary>   
  212. </STRONG>    <STRONG class=c>/// 这公有方法提取本网页的纯文本中满足某正则式的文字   
  213. </STRONG>    <STRONG class=c>/// </summary>   
  214. </STRONG>    <STRONG class=c>/// <param name="pattern">正则式</param>   
  215. </STRONG>    <STRONG class=c>/// <returns>返回文字</returns>   
  216. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> getSpecialWords(<STRONG class=k>string</STRONG> pattern)   
  217.     {   
  218.         <STRONG class=k>if</STRONG> (m_outstr == <STRONG class=s>""</STRONG>) getContext(Int16.MaxValue);   
  219.         Regex regex = <STRONG class=k>new</STRONG> Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase );   
  220.         Match mc=regex.Match(m_outstr);   
  221.         <STRONG class=k>if</STRONG> (mc.Success)   
  222.             <STRONG class=k>return</STRONG> mc.Groups[1].Value;   
  223.         <STRONG class=k>return</STRONG> <STRONG class=k>string</STRONG>.Empty;   
  224.     }   
  225. <STRONG class=r>    #endregion   
  226. </STRONG><STRONG class=r>  
  227.  
  228.     #region 构造函数   
  229. </STRONG>       
  230.     <STRONG class=k>private</STRONG> <STRONG class=k>void</STRONG> Init(<STRONG class=k>string</STRONG> _url)   
  231.     {   
  232.       
  233.         <STRONG class=k>try</STRONG>   
  234.         {   
  235.             m_uri = <STRONG class=k>new</STRONG> Uri(_url);   
  236.             m_links = <STRONG class=k>new</STRONG> List<Link>();   
  237.             m_html = <STRONG class=s>""</STRONG>;   
  238.             m_outstr = <STRONG class=s>""</STRONG>;   
  239.             m_title = <STRONG class=s>""</STRONG>;   
  240.             m_good = <STRONG class=k>true</STRONG>;   
  241.             <STRONG class=k>if</STRONG> (_url.EndsWith(<STRONG class=s>".rar"</STRONG>) || _url.EndsWith(<STRONG class=s>".dat"</STRONG>) || _url.EndsWith(<STRONG class=s>".msi"</STRONG>))   
  242.             {   
  243.                 m_good = <STRONG class=k>false</STRONG>;   
  244.                 <STRONG class=k>return</STRONG>;   
  245.             }   
  246.             HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(m_uri);   
  247.             rqst.AllowAutoRedirect = <STRONG class=k>true</STRONG>;   
  248.             rqst.MaximumAutomaticRedirections = 3;   
  249.             rqst.UserAgent = <STRONG class=s>"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"</STRONG>;   
  250.             rqst.KeepAlive = <STRONG class=k>true</STRONG>;   
  251.             rqst.Timeout = 30000;   
  252.             <STRONG class=k>lock</STRONG> (WebPage.webcookies)   
  253.             {   
  254.                 <STRONG class=k>if</STRONG> (WebPage.webcookies.ContainsKey(m_uri.Host))   
  255.                     rqst.CookieContainer = WebPage.webcookies[m_uri.Host];   
  256.                 <STRONG class=k>else</STRONG>   
  257.                 {   
  258.                     CookieContainer cc = <STRONG class=k>new</STRONG> CookieContainer();   
  259.                     WebPage.webcookies[m_uri.Host] = cc;   
  260.                     rqst.CookieContainer = cc;   
  261.                 }   
  262.             }   
  263.   
  264.             HttpWebResponse rsps = (HttpWebResponse)rqst.GetResponse();   
  265.   
  266.             Stream sm = rsps.GetResponseStream();   
  267.             <STRONG class=k>if</STRONG> (!rsps.ContentType.ToLower().StartsWith(<STRONG class=s>"text/"</STRONG>) || rsps.ContentLength > 1 << 22)   
  268.             {   
  269.                 rsps.Close();   
  270.                 m_good = <STRONG class=k>false</STRONG>;   
  271.                 <STRONG class=k>return</STRONG>;   
  272.             }   
  273.             Encoding cding = System.Text.Encoding.Default;   
  274.             <STRONG class=k>string</STRONG> contenttype=rsps.ContentType.ToLower();   
  275.             <STRONG class=k>int</STRONG> ix = contenttype.IndexOf(<STRONG class=s>"charset="</STRONG>);   
  276.             <STRONG class=k>if</STRONG> (ix != -1)   
  277.             {   
  278.   
  279.                 <STRONG class=k>try</STRONG>   
  280.                 {   
  281.                     cding = System.Text.Encoding.GetEncoding(rsps.ContentType.Substring(ix + <STRONG class=s>"charset"</STRONG>.Length + 1));   
  282.                 }   
  283.                 <STRONG class=k>catch</STRONG>   
  284.                 {   
  285.                     cding = Encoding.Default;   
  286.                 }   
  287.                 m_html = <STRONG class=k>new</STRONG> StreamReader(sm, cding).ReadToEnd();   
  288.             }   
  289.             <STRONG class=k>else</STRONG>   
  290.             {   
  291.                 m_html = <STRONG class=k>new</STRONG> StreamReader(sm, cding).ReadToEnd();   
  292.                 Regex regex = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>"charset=(?<cding>[^=]+)?\""</STRONG>,RegexOptions.IgnoreCase);   
  293.                 <STRONG class=k>string</STRONG> strcding = regex.Match(m_html).Groups[<STRONG class=s>"cding"</STRONG>].Value;   
  294.                 <STRONG class=k>try</STRONG>   
  295.                 {   
  296.                     cding = Encoding.GetEncoding(strcding);   
  297.                 }   
  298.                 <STRONG class=k>catch</STRONG>{   
  299.                     cding = Encoding.Default;   
  300.                 }   
  301.                 <STRONG class=k>byte</STRONG>[] bytes=Encoding.Default.GetBytes(m_html.ToCharArray());   
  302.                 m_html = cding.GetString(bytes);   
  303.                 <STRONG class=k>if</STRONG> (m_html.Split(<STRONG class=s>'?'</STRONG>).Length > 100)   
  304.                 {   
  305.                     m_html=Encoding.Default.GetString(bytes);   
  306.                 }   
  307.             }   
  308.   
  309.                
  310.             m_pagesize = m_html.Length;   
  311.             m_uri = rsps.ResponseUri;   
  312.             rsps.Close();   
  313.         }   
  314.         <STRONG class=k>catch</STRONG> (Exception ex)   
  315.         {   
  316.             Console.WriteLine(ex.Message+m_uri.ToString());   
  317.             m_good = <STRONG class=k>false</STRONG>;   
  318.                
  319.         }   
  320.     }   
  321.   
  322.     <STRONG class=k>public</STRONG> WebPage(<STRONG class=k>string</STRONG> _url)   
  323.     {   
  324.         <STRONG class=k>string</STRONG> uurl = <STRONG class=s>""</STRONG>;   
  325.         <STRONG class=k>try</STRONG>   
  326.         {   
  327.             uurl = Uri.UnescapeDataString(_url);   
  328.             _url = uurl;   
  329.         }   
  330.         <STRONG class=k>catch</STRONG> { };   
  331.         Regex re = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>"(?<h>[^\x00-\xff]+)"</STRONG>);   
  332.         Match mc = re.Match(_url);   
  333.         <STRONG class=k>if</STRONG> (mc.Success)   
  334.         {   
  335.             <STRONG class=k>string</STRONG> han = mc.Groups[<STRONG class=s>"h"</STRONG>].Value;   
  336.             _url = _url.Replace(han, System.Web.HttpUtility.UrlEncode(han, Encoding.GetEncoding(<STRONG class=s>"GB2312"</STRONG>)));   
  337.         }   
  338.   
  339.         Init(_url);   
  340.     }   
  341.   
  342.     <STRONG class=k>public</STRONG> WebPage(<STRONG class=k>string</STRONG> _url, <STRONG class=k>string</STRONG> _loginurl, <STRONG class=k>string</STRONG> _post)   
  343.     {   
  344.         <STRONG class=k>string</STRONG> uurl = <STRONG class=s>""</STRONG>;   
  345.         <STRONG class=k>try</STRONG>   
  346.         {   
  347.             uurl = Uri.UnescapeDataString(_url);   
  348.             _url = uurl;   
  349.         }   
  350.         <STRONG class=k>catch</STRONG> { };   
  351.         Regex re = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>"(?<h>[^\x00-\xff]+)"</STRONG>);   
  352.         Match mc = re.Match(_url);   
  353.         <STRONG class=k>if</STRONG> (mc.Success)   
  354.         {   
  355.             <STRONG class=k>string</STRONG> han = mc.Groups[<STRONG class=s>"h"</STRONG>].Value;   
  356.             _url = _url.Replace(han, System.Web.HttpUtility.UrlEncode(han, Encoding.GetEncoding(<STRONG class=s>"GB2312"</STRONG>)));   
  357.         }   
  358.         <STRONG class=k>if</STRONG> (_loginurl.Trim() == <STRONG class=s>""</STRONG> || _post.Trim() == <STRONG class=s>""</STRONG> || WebPage.webcookies.ContainsKey(<STRONG class=k>new</STRONG> Uri(_url).Host))   
  359.         {   
  360.             Init(_url);   
  361.         }   
  362.         <STRONG class=k>else</STRONG>   
  363.         {   
  364. <STRONG class=r>            #region 登陆   
  365. </STRONG>            <STRONG class=k>string</STRONG> indata = _post;   
  366.             m_post = _post;   
  367.             m_loginurl = _loginurl;   
  368.             <STRONG class=k>byte</STRONG>[] bytes = Encoding.Default.GetBytes(_post);   
  369.             CookieContainer myCookieContainer = <STRONG class=k>new</STRONG> CookieContainer();   
  370.             <STRONG class=k>try</STRONG>   
  371.             {   
  372.   
  373.                 <STRONG class=c>//新建一个CookieContainer来存放Cookie集合    
  374. </STRONG>   
  375.                 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(_loginurl);   
  376.                 <STRONG class=c>//新建一个HttpWebRequest    
  377. </STRONG>                myHttpWebRequest.ContentType = <STRONG class=s>"application/x-www-form-urlencoded"</STRONG>;   
  378.                 myHttpWebRequest.AllowAutoRedirect = <STRONG class=k>false</STRONG>;   
  379.                 myHttpWebRequest.UserAgent = <STRONG class=s>"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"</STRONG>;   
  380.                 myHttpWebRequest.Timeout = 60000;   
  381.                 myHttpWebRequest.KeepAlive = <STRONG class=k>true</STRONG>;   
  382.                 myHttpWebRequest.ContentLength = bytes.Length;   
  383.                 myHttpWebRequest.Method = <STRONG class=s>"POST"</STRONG>;   
  384.                 myHttpWebRequest.CookieContainer = myCookieContainer;   
  385.                 <STRONG class=c>//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer    
  386. </STRONG>                Stream myRequestStream = myHttpWebRequest.GetRequestStream();   
  387.                 myRequestStream.Write(bytes, 0, bytes.Length);   
  388.                 myRequestStream.Close();   
  389.                 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();   
  390.   
  391.                 <STRONG class=k>foreach</STRONG> (Cookie ck <STRONG class=k>in</STRONG> myHttpWebResponse.Cookies)   
  392.                 {   
  393.                     myCookieContainer.Add(ck);   
  394.                 }   
  395.                 myHttpWebResponse.Close();   
  396.             }   
  397.             <STRONG class=k>catch</STRONG>   
  398.             {   
  399.                 Init(_url);   
  400.                 <STRONG class=k>return</STRONG>;   
  401.             }   
  402. <STRONG class=r>  
  403.             #endregion   
  404. </STRONG><STRONG class=r>  
  405.             #region 登陆后再访问页面   
  406. </STRONG>            <STRONG class=k>try</STRONG>   
  407.             {   
  408.                 m_uri = <STRONG class=k>new</STRONG> Uri(_url);   
  409.                 m_links = <STRONG class=k>new</STRONG> List<Link>();   
  410.                 m_html = <STRONG class=s>""</STRONG>;   
  411.                 m_outstr = <STRONG class=s>""</STRONG>;   
  412.                 m_title = <STRONG class=s>""</STRONG>;   
  413.                 m_good = <STRONG class=k>true</STRONG>;   
  414.                 <STRONG class=k>if</STRONG> (_url.EndsWith(<STRONG class=s>".rar"</STRONG>) || _url.EndsWith(<STRONG class=s>".dat"</STRONG>) || _url.EndsWith(<STRONG class=s>".msi"</STRONG>))   
  415.                 {   
  416.                     m_good = <STRONG class=k>false</STRONG>;   
  417.                     <STRONG class=k>return</STRONG>;   
  418.                 }   
  419.                 HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(m_uri);   
  420.                 rqst.AllowAutoRedirect = <STRONG class=k>true</STRONG>;   
  421.                 rqst.MaximumAutomaticRedirections = 3;   
  422.                 rqst.UserAgent = <STRONG class=s>"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"</STRONG>;   
  423.                 rqst.KeepAlive = <STRONG class=k>true</STRONG>;   
  424.                 rqst.Timeout = 30000;   
  425.                 rqst.CookieContainer = myCookieContainer;   
  426.                 <STRONG class=k>lock</STRONG> (WebPage.webcookies)   
  427.                 {   
  428.                     WebPage.webcookies[m_uri.Host] = myCookieContainer;   
  429.                 }   
  430.                 HttpWebResponse rsps = (HttpWebResponse)rqst.GetResponse();   
  431.   
  432.                 Stream sm = rsps.GetResponseStream();   
  433.                 <STRONG class=k>if</STRONG> (!rsps.ContentType.ToLower().StartsWith(<STRONG class=s>"text/"</STRONG>) || rsps.ContentLength > 1 << 22)   
  434.                 {   
  435.                     rsps.Close();   
  436.                     m_good = <STRONG class=k>false</STRONG>;   
  437.                     <STRONG class=k>return</STRONG>;   
  438.                 }   
  439.                 Encoding cding = System.Text.Encoding.Default;   
  440.                 <STRONG class=k>int</STRONG> ix = rsps.ContentType.ToLower().IndexOf(<STRONG class=s>"charset="</STRONG>);   
  441.                 <STRONG class=k>if</STRONG> (ix != -1)   
  442.                 {   
  443.                     <STRONG class=k>try</STRONG>   
  444.                     {   
  445.                         cding = System.Text.Encoding.GetEncoding(rsps.ContentType.Substring(ix + <STRONG class=s>"charset"</STRONG>.Length + 1));   
  446.                     }   
  447.                     <STRONG class=k>catch</STRONG>   
  448.                     {   
  449.                         cding = Encoding.Default;   
  450.                     }   
  451.                 }   
  452.   
  453.   
  454.                 m_html = <STRONG class=k>new</STRONG> StreamReader(sm, cding).ReadToEnd();   
  455.   
  456.   
  457.                 m_pagesize = m_html.Length;   
  458.                 m_uri = rsps.ResponseUri;   
  459.                 rsps.Close();   
  460.             }   
  461.             <STRONG class=k>catch</STRONG> (Exception ex)   
  462.             {   
  463.                 Console.WriteLine(ex.Message+m_uri.ToString());   
  464.                 m_good = <STRONG class=k>false</STRONG>;   
  465.                
  466.             }   
  467. <STRONG class=r>            #endregion   
  468. </STRONG>        }   
  469.   
  470.     }   
  471. <STRONG class=r>  
  472.     #endregion   
  473. </STRONG><STRONG class=r>  
  474.  
  475.     #region 属性   
  476. </STRONG>   
  477.     <STRONG class=c>/// <summary>   
  478. </STRONG>    <STRONG class=c>/// 通过此属性可获得本网页的网址,只读   
  479. </STRONG>    <STRONG class=c>/// </summary>   
  480. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> URL   
  481.     {   
  482.         <STRONG class=k>get</STRONG>   
  483.         {   
  484.             <STRONG class=k>return</STRONG> m_uri.AbsoluteUri;   
  485.         }   
  486.     }   
  487.   
  488.     <STRONG class=c>/// <summary>   
  489. </STRONG>    <STRONG class=c>/// 通过此属性可获得本网页的标题,只读   
  490. </STRONG>    <STRONG class=c>/// </summary>   
  491. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> Title   
  492.     {   
  493.         <STRONG class=k>get</STRONG>   
  494.         {   
  495.             <STRONG class=k>if</STRONG> (m_title == <STRONG class=s>""</STRONG>)   
  496.             {   
  497.                 Regex reg = <STRONG class=k>new</STRONG> Regex(<STRONG class=s>@"(?m)<title[^>]*>(?<title>(?:\w|\W)*?)</title[^>]*>"</STRONG>, RegexOptions.Multiline | RegexOptions.IgnoreCase );   
  498.                 Match mc = reg.Match(m_html);   
  499.                 <STRONG class=k>if</STRONG> (mc.Success)   
  500.                     m_title= mc.Groups[<STRONG class=s>"title"</STRONG>].Value.Trim();   
  501.             }   
  502.             <STRONG class=k>return</STRONG> m_title;   
  503.         }   
  504.     }   
  505.      
  506.   
  507.     <STRONG class=c>/// <summary>   
  508. </STRONG>    <STRONG class=c>/// 此属性获得本网页的所有链接信息,只读   
  509. </STRONG>    <STRONG class=c>/// </summary>   
  510. </STRONG>    <STRONG class=k>public</STRONG> List<Link> Links   
  511.     {   
  512.         <STRONG class=k>get</STRONG>   
  513.         {   
  514.             <STRONG class=k>if</STRONG> (m_links.Count == 0) getLinks();   
  515.             <STRONG class=k>return</STRONG> m_links;   
  516.         }   
  517.     }   
  518.   
  519.   
  520.     <STRONG class=c>/// <summary>   
  521. </STRONG>    <STRONG class=c>/// 此属性返回本网页的全部纯文本信息,只读   
  522. </STRONG>    <STRONG class=c>/// </summary>   
  523. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> Context   
  524.     {   
  525.        <STRONG class=k>get</STRONG>   
  526.        {   
  527.            <STRONG class=k>if</STRONG> (m_outstr == <STRONG class=s>""</STRONG>) getContext(Int16.MaxValue);   
  528.            <STRONG class=k>return</STRONG> m_outstr;   
  529.        }   
  530.     }   
  531.   
  532.     <STRONG class=c>/// <summary>   
  533. </STRONG>    <STRONG class=c>/// 此属性获得本网页的大小   
  534. </STRONG>    <STRONG class=c>/// </summary>   
  535. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>int</STRONG> PageSize   
  536.     {   
  537.         <STRONG class=k>get</STRONG>   
  538.         {   
  539.             <STRONG class=k>return</STRONG> m_pagesize;   
  540.         }   
  541.     }   
  542.     <STRONG class=c>/// <summary>   
  543. </STRONG>    <STRONG class=c>/// 此属性获得本网页的所有站内链接   
  544. </STRONG>    <STRONG class=c>/// </summary>   
  545. </STRONG>    <STRONG class=k>public</STRONG> List<Link> InsiteLinks   
  546.     {   
  547.         <STRONG class=k>get</STRONG>   
  548.         {   
  549.             <STRONG class=k>return</STRONG> getSpecialLinksByUrl("^http:<STRONG class=c>//"+m_uri.Host,Int16.MaxValue);   
  550. </STRONG>        }   
  551.     }   
  552.   
  553.     <STRONG class=c>/// <summary>   
  554. </STRONG>    <STRONG class=c>/// 此属性表示本网页是否可用   
  555. </STRONG>    <STRONG class=c>/// </summary>   
  556. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>bool</STRONG> IsGood   
  557.     {   
  558.         <STRONG class=k>get</STRONG>   
  559.         {   
  560.             <STRONG class=k>return</STRONG> m_good;   
  561.         }   
  562.     }   
  563.     <STRONG class=c>/// <summary>   
  564. </STRONG>    <STRONG class=c>/// 此属性表示网页的所在的网站   
  565. </STRONG>    <STRONG class=c>/// </summary>   
  566. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> Host   
  567.     {   
  568.         <STRONG class=k>get</STRONG>   
  569.         {   
  570.             <STRONG class=k>return</STRONG> m_uri.Host;   
  571.         }   
  572.     }   
  573.        
  574.   
  575.     <STRONG class=c>/// <summary>   
  576. </STRONG>    <STRONG class=c>/// 此网页的登陆页所需的POST数据   
  577. </STRONG>    <STRONG class=c>/// </summary>   
  578. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> PostStr   
  579.     {   
  580.         <STRONG class=k>get</STRONG>   
  581.         {   
  582.             <STRONG class=k>return</STRONG> m_post;   
  583.         }   
  584.     }   
  585.     <STRONG class=c>/// <summary>   
  586. </STRONG>    <STRONG class=c>/// 此网页的登陆页   
  587. </STRONG>    <STRONG class=c>/// </summary>   
  588. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> LoginURL   
  589.     {   
  590.         <STRONG class=k>get</STRONG>   
  591.         {   
  592.             <STRONG class=k>return</STRONG> m_loginurl;   
  593.         }   
  594.     }   
  595. <STRONG class=r>    #endregion   
  596. </STRONG>}   
  597.   
  598. <STRONG class=c>/// <summary>   
  599. </STRONG><STRONG class=c>/// 链接类   
  600. </STRONG><STRONG class=c>/// </summary>   
  601. </STRONG><STRONG class=k>public</STRONG> <STRONG class=k>class</STRONG> Link   
  602. {   
  603.     <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> url;   <STRONG class=c>//链接网址   
  604. </STRONG>    <STRONG class=k>public</STRONG> <STRONG class=k>string</STRONG> text;  <STRONG class=c>//链接文字   
  605. </STRONG>    <STRONG class=k>public</STRONG> Link(<STRONG class=k>string</STRONG> _url, <STRONG class=k>string</STRONG> _text)   
  606.     {   
  607.         url = _url;   
  608.         text = _text;   
  609.     }   
  610. }  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
得到网页的最新更新时间
调试信息的输出
使用VC6.0对MFC源代码挖掘 - iwknow的专栏 - CSDN博客 - dingc...
JAVAannotation入门
装饰模式(DECORATOR)案例分析 - jia
Android学习进阶路线导航线路(Android源码分享)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服