Asp.net 数据采集基类(远程抓取,分解,保存,匹配)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值