asp.net采集函数

来源:互联网 发布:怎么在淘宝免费买东西 编辑:程序博客网 时间:2024/05/15 05:09
  1. using System; 
  2. using System.Data; 
  3. using System.Configuration; 
  4. using System.Web; 
  5. using System.Web.Security; 
  6. using System.Web.UI; 
  7. using System.Web.UI.WebControls; 
  8. using System.Web.UI.WebControls.WebParts; 
  9. using System.Web.UI.HtmlControls; 
  10. using MSXML2; 
  11. using System.Text.RegularExpressions; 
  12. namespace EC 
  13.     /// 
  14.     /// 远程文件抓取类 
  15.     /// 
  16.     public class GetRemoteObj 
  17.     { 
  18.         #region 构造与析构函数 
  19.         public GetRemoteObj() 
  20.         { 
  21.             // 
  22.             // TODO: 在此处添加构造函数逻辑 
  23.             // 
  24.         } 
  25.         ~GetRemoteObj() 
  26.         { 
  27.             Dispose(); 
  28.         } 
  29.         #endregion 
  30.         #region IDisposable 成员 
  31.         public void Dispose() 
  32.         { 
  33.             GC.SuppressFinalize(this); 
  34.         } 
  35.         #endregion 
  36.         #region 日期随机函数 
  37.         /********************************** 
  38.          * 函数名称:DateRndName 
  39.          * 功能说明:日期随机函数 
  40.          * 参    数:ra:随机数 
  41.          * 调用示例: 
  42.          *          GetRemoteObj o = new GetRemoteObj(); 
  43.          *          Random ra = new Random(); 
  44.          *          string s = o.DateRndName(ra); 
  45.          *          Response.Write(s); 
  46.          *          o.Dispose(); 
  47.          * ********************************/ 
  48.         /// 
  49.         /// 日期随机函数 
  50.         /// 
  51.         /// 随机数 
  52.         /// 
  53.         public string DateRndName(Random ra) 
  54.         { 
  55.             DateTime d = DateTime.Now; 
  56.             string s = null, y, m, dd, h, mm, ss; 
  57.             y = d.Year.ToString(); 
  58.             m = d.Month.ToString(); 
  59.             if (m.Length < 2) m = "0" + m; 
  60.             dd = d.Day.ToString(); 
  61.             if (dd.Length < 2) dd = "0" + dd; 
  62.             h = d.Hour.ToString(); 
  63.             if (h.Length < 2) h = "0" + h; 
  64.             mm = d.Minute.ToString(); 
  65.             if (mm.Length < 2) mm = "0" + mm; 
  66.             ss = d.Second.ToString(); 
  67.             if (ss.Length < 2) ss = "0" + ss; 
  68.             s += y + m + dd + h + mm + ss; 
  69.             s += ra.Next(100, 999).ToString(); 
  70.             return s; 
  71.         } 
  72.         #endregion 
  73.         #region 取得文件后缀 
  74.         /********************************** 
  75.          * 函数名称:GetFileExtends 
  76.          * 功能说明:取得文件后缀 
  77.          * 参    数:filename:文件名称 
  78.          * 调用示例: 
  79.          *          GetRemoteObj o = new GetRemoteObj(); 
  80.          *          string url = @"http://www.baidu.com/img/logo.gif"; 
  81.          *          string s = o.GetFileExtends(url); 
  82.          *          Response.Write(s); 
  83.          *          o.Dispose(); 
  84.          * ********************************/ 
  85.         /// 
  86.         /// 取得文件后缀 
  87.         /// 
  88.         /// 文件名称 
  89.         /// 
  90.         public string GetFileExtends(string filename) 
  91.         { 
  92.             string ext = null
  93.             if (filename.IndexOf('.') > 0) 
  94.             { 
  95.                 string[] fs = filename.Split('.'); 
  96.                 ext = fs[fs.Length - 1]; 
  97.             } 
  98.             return ext; 
  99.         } 
  100.         #endregion 
  101.         #region 获取远程文件源代码 
  102.         /********************************** 
  103.          * 函数名称:GetRemoteHtmlCode 
  104.          * 功能说明:获取远程文件源代码 
  105.          * 参    数:Url:远程url 
  106.          * 调用示例: 
  107.          *          GetRemoteObj o = new GetRemoteObj(); 
  108.          *          string url = @"http://www.baidu.com"; 
  109.          *          string s = o.GetRemoteHtmlCode(url); 
  110.          *          Response.Write(s); 
  111.          *          o.Dispose(); 
  112.          * ********************************/ 
  113.         /// 
  114.         /// 获取远程文件源代码 
  115.         /// 
  116.         /// 远程url 
  117.         /// 
  118.         public string GetRemoteHtmlCode(string Url) 
  119.         { 
  120.             string s = ""
  121.             MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass(); 
  122.             _xmlhttp.open("GET", Url, falsenullnull); 
  123.             _xmlhttp.send(""); 
  124.             if (_xmlhttp.readyState == 4) 
  125.             { 
  126.                 s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody); 
  127.             } 
  128.             return s; 
  129.         } 
  130.         #endregion 
  131.         #region 保存远程文件 
  132.         /********************************** 
  133.          * 函数名称:RemoteSave 
  134.          * 功能说明:保存远程文件 
  135.          * 参    数:Url:远程url;Path:保存到的路径 
  136.          * 调用示例: 
  137.          *          GetRemoteObj o = new GetRemoteObj(); 
  138.          *          string s = ""; 
  139.          *          string url = @"http://www.baidu.com/img/logo.gif"; 
  140.          *          string path =Server.MapPath("Html/"); 
  141.          *          s = o.RemoteSave(url,path); 
  142.          *          Response.Write(s); 
  143.          *          o.Dispose();          
  144.          * ******************************/ 
  145.         /// 
  146.         /// 保存远程文件 
  147.         /// 
  148.         /// 远程url 
  149.         /// 保存到的路径 
  150.         /// 
  151.         public string RemoteSave(string Url, string Path) 
  152.         { 
  153.             Random ra = new Random(); 
  154.             string StringFileName = DateRndName(ra) + "." + GetFileExtends(Url); 
  155.             string StringFilePath = Path + StringFileName; 
  156.             MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass(); 
  157.             _xmlhttp.open("GET", Url, falsenullnull); 
  158.             _xmlhttp.send(""); 
  159.             if (_xmlhttp.readyState == 4) 
  160.             { 
  161.                 if (System.IO.File.Exists(StringFilePath)) 
  162.                     System.IO.File.Delete(StringFilePath); 
  163.                 System.IO.FileStream fs = new System.IO.FileStream(StringFilePath, System.IO.FileMode.CreateNew); 
  164.                 System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs); 
  165.                 w.Write((byte[])_xmlhttp.responseBody); 
  166.                 w.Close(); 
  167.                 fs.Close(); 
  168.             } 
  169.             else 
  170.                 throw new Exception(_xmlhttp.statusText); 
  171.             return StringFileName; 
  172.         } 
  173.         #endregion 
  174.         #region 替换网页中的换行和引号 
  175.         /********************************** 
  176.          * 函数名称:ReplaceEnter 
  177.          * 功能说明:替换网页中的换行和引号 
  178.          * 参    数:HtmlCode:html源代码 
  179.          * 调用示例: 
  180.          *          GetRemoteObj o = new GetRemoteObj(); 
  181.          *          string Url = @"http://www.baidu.com"; 
  182.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  183.          *          string s = o.ReplaceEnter(HtmlCode); 
  184.          *          Response.Write(s); 
  185.          *          o.Dispose(); 
  186.          * ********************************/ 
  187.         /// 
  188.         /// 替换网页中的换行和引号 
  189.         /// 
  190.         /// HTML源代码 
  191.         /// 
  192.         public string ReplaceEnter(string HtmlCode) 
  193.         { 
  194.             string s = ""
  195.             if (HtmlCode == null || HtmlCode == ""
  196.                 s = ""
  197.             else 
  198.                 s = HtmlCode.Replace("/""""); 
  199.             s = s.Replace("/r/n"""); 
  200.             return s; 
  201.         } 
  202.         #endregion 
  203.         #region 执行正则提取出值 
  204.         /********************************** 
  205.          * 函数名称:GetRegValue 
  206.          * 功能说明:执行正则提取出值 
  207.          * 参    数:HtmlCode:html源代码 
  208.          * 调用示例: 
  209.          *          GetRemoteObj o = new GetRemoteObj(); 
  210.          *          string Url = @"http://www.baidu.com"; 
  211.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  212.          *          string s = o.ReplaceEnter(HtmlCode); 
  213.          *          string Reg=""; 
  214.          *          string GetValue=o.GetRegValue(Reg,HtmlCode) 
  215.          *          Response.Write(GetValue); 
  216.          *          o.Dispose(); 
  217.          * ********************************/ 
  218.         /// 
  219.         /// 执行正则提取出值 
  220.         /// 
  221.         /// 正则表达式 
  222.         /// HtmlCode源代码 
  223.         /// 
  224.         public string GetRegValue(string RegexString, string RemoteStr) 
  225.         { 
  226.             string MatchVale = ""
  227.             Regex r = new Regex(RegexString); 
  228.             Match m = r.Match(RemoteStr); 
  229.             if (m.Success) 
  230.             { 
  231.                 MatchVale = m.Value; 
  232.             } 
  233.             return MatchVale; 
  234.         } 
  235.         #endregion 
  236.         #region 替换HTML源代码 
  237.         /********************************** 
  238.          * 函数名称:RemoveHTML 
  239.          * 功能说明:替换HTML源代码 
  240.          * 参    数:HtmlCode:html源代码 
  241.          * 调用示例: 
  242.          *          GetRemoteObj o = new GetRemoteObj(); 
  243.          *          string Url = @"http://www.baidu.com"; 
  244.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  245.          *          string s = o.ReplaceEnter(HtmlCode); 
  246.          *          string Reg=""; 
  247.          *          string GetValue=o.GetRegValue(Reg,HtmlCode) 
  248.          *          Response.Write(GetValue); 
  249.          *          o.Dispose(); 
  250.          * ********************************/ 
  251.         /// 
  252.         /// 替换HTML源代码 
  253.         /// 
  254.         /// html源代码 
  255.         /// 
  256.         public string RemoveHTML(string HtmlCode) 
  257.         { 
  258.             string MatchVale = HtmlCode; 
  259.             foreach (Match s in Regex.Matches(HtmlCode, "<.+?>")) 
  260.             { 
  261.                 MatchVale = MatchVale.Replace(s.Value, ""); 
  262.             } 
  263.             return MatchVale; 
  264.         } 
  265.         #endregion 
  266.         #region 匹配页面的链接 
  267.         /********************************** 
  268.          * 函数名称:GetHref 
  269.          * 功能说明:匹配页面的链接 
  270.          * 参    数:HtmlCode:html源代码 
  271.          * 调用示例: 
  272.          *          GetRemoteObj o = new GetRemoteObj(); 
  273.          *          string Url = @"http://www.baidu.com"; 
  274.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  275.          *          string s = o.GetHref(HtmlCode); 
  276.          *          Response.Write(s); 
  277.          *          o.Dispose(); 
  278.          * ********************************/ 
  279.         /// 
  280.         /// 获取页面的链接正则 
  281.         /// 
  282.         /// 
  283.         /// 
  284.         public string GetHref(string HtmlCode) 
  285.         { 
  286.             string MatchVale = ""
  287.             string Reg = @"(h|H)(r|R)(e|E)(f|F) *= *('|"")?((/w|//|//|/.|:|-|_)+)('|""| *|>)?"
  288.             foreach (Match m in Regex.Matches(HtmlCode, Reg)) 
  289.             { 
  290.                 MatchVale += (m.Value).ToLower().Replace("href=""").Trim() + "||"
  291.             } 
  292.             return MatchVale; 
  293.         } 
  294.         #endregion 
  295.         #region 匹配页面的图片地址 
  296.         /********************************** 
  297.          * 函数名称:GetImgSrc 
  298.          * 功能说明:匹配页面的图片地址 
  299.          * 参    数:HtmlCode:html源代码;imgHttp:要补充的http.当比如:则要补充http://www.baidu.com/,当包含http信息时,则可以为空 
  300.          * 调用示例: 
  301.          *          GetRemoteObj o = new GetRemoteObj(); 
  302.          *          string Url = @"http://www.baidu.com"; 
  303.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  304.          *          string s = o.GetImgSrc(HtmlCode,"http://www.baidu.com/"); 
  305.          *          Response.Write(s); 
  306.          *          o.Dispose(); 
  307.          * ********************************/ 
  308.         /// 
  309.         /// 匹配页面的图片地址 
  310.         /// 
  311.         /// 
  312.         /// 要补充的http://路径信息 
  313.         /// 
  314.         public string GetImgSrc(string HtmlCode, string imgHttp) 
  315.         { 
  316.             string MatchVale = ""
  317.             string Reg = @""
  318.             foreach (Match m in Regex.Matches(HtmlCode, Reg)) 
  319.             { 
  320.                 MatchVale += GetImg((m.Value).ToLower().Trim(), imgHttp) + "||"
  321.             } 
  322.             return MatchVale; 
  323.         } 
  324.         /// 
  325.         /// 匹配中的图片路径实际链接 
  326.         /// 
  327.         /// 字符串 
  328.         /// 
  329.         public string GetImg(string ImgString, string imgHttp) 
  330.         { 
  331.             string MatchVale = ""
  332.             string Reg = @"src=.+/.(bmp|jpg|gif|png|)"
  333.             foreach (Match m in Regex.Matches(ImgString.ToLower(), Reg)) 
  334.             { 
  335.                 MatchVale += (m.Value).ToLower().Trim().Replace("src="""); 
  336.             } 
  337.             return (imgHttp + MatchVale); 
  338.         } 
  339.         #endregion 
  340.         #region 替换通过正则获取字符串所带的正则首尾匹配字符串 
  341.         /********************************** 
  342.          * 函数名称:GetHref 
  343.          * 功能说明:匹配页面的链接 
  344.          * 参    数:HtmlCode:html源代码 
  345.          * 调用示例: 
  346.          *          GetRemoteObj o = new GetRemoteObj(); 
  347.          *          string Url = @"http://www.baidu.com"; 
  348.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  349.          *          string s = o.RegReplace(HtmlCode,""); 
  350.          *          Response.Write(s); 
  351.          *          o.Dispose(); 
  352.          * ********************************/ 
  353.         /// 
  354.         /// 替换通过正则获取字符串所带的正则首尾匹配字符串 
  355.         /// 
  356.         /// 要替换的值 
  357.         /// 正则匹配的首字符串 
  358.         /// 正则匹配的尾字符串 
  359.         /// 
  360.         public string RegReplace(string RegValue, string regStart, string regEnd) 
  361.         { 
  362.             string s = RegValue; 
  363.             if (RegValue != "" && RegValue != null
  364.             { 
  365.                 if (regStart != "" && regStart != null
  366.                 { 
  367.                     s = s.Replace(regStart, ""); 
  368.                 } 
  369.                 if (regEnd != "" && regEnd != null
  370.                 { 
  371.                     s = s.Replace(regEnd, ""); 
  372.                 } 
  373.             } 
  374.             return s; 
  375.         } 
  376.         #endregion 
  377.     } 
  378. 记得添加引用 Interop.MSXML2.dll 否则msxml2命名空间用不了
原创粉丝点击