資料采集基類[轉]
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MSXML2; // 添加引用、COM、Microsoft XML , v2.6、選擇、确定。
using System.Text.RegularExpressions;
/// <summary>
/// 遠端檔案抓取類
/// </summary>
public class GetRemoteObj
{
#region 構造與析構函數
public GetRemoteObj()
{
//
// TODO: 在此處添加構造函數邏輯
}
~GetRemoteObj()
Dispose();
#endregion
#region IDisposable 成員
public void Dispose()
GC.SuppressFinalize(this);
#region 日期随機函數
/**********************************
* 函數名稱:DateRndName
* 功能說明:日期随機函數
* 參 數:ra:随機數
* 調用示例:
* GetRemoteObj o = new GetRemoteObj();
* Random ra = new Random();
* string s = o.DateRndName(ra);
* Response.Write(s);
* o.Dispose();
* ********************************/
/// 日期随機函數
/// <param name="ra">随機數</param>
/// <returns></returns>
public string DateRndName(Random ra)
DateTime d = DateTime.Now;
string s = null, y, m, dd, h, mm, ss;
y = d.Year.ToString();
m = d.Month.ToString();
if (m.Length < 2) m = "0" + m;
dd = d.Day.ToString();
if (dd.Length < 2) dd = "0" + dd;
h = d.Hour.ToString();
if (h.Length < 2) h = "0" + h;
mm = d.Minute.ToString();
if (mm.Length < 2) mm = "0" + mm;
ss = d.Second.ToString();
if (ss.Length < 2) ss = "0" + ss;
s += y + m + dd + h + mm + ss;
s += ra.Next(100, 999).ToString();
return s;
#region 取得檔案字尾
* 函數名稱:GetFileExtends
* 功能說明:取得檔案字尾
* 參 數:filename:檔案名稱
* string url = @"http://www.baidu.com/img/logo.gif";
* string s = o.GetFileExtends(url);
/// 取得檔案字尾
/// <param name="filename">檔案名稱</param>
public string GetFileExtends(string filename)
string ext = null;
if (filename.IndexOf('.') > 0)
{
string[] fs = filename.Split('.');
ext = fs[fs.Length - 1];
}
return ext;
#region 擷取遠端檔案源代碼
* 函數名稱:GetRemoteHtmlCode
* 功能說明:擷取遠端檔案源代碼
* 參 數:Url:遠端url
* string url = @"http://www.baidu.com/";
* string s = o.GetRemoteHtmlCode(url);
/// 擷取遠端檔案源代碼
/// <param name="url">遠端url</param>
public string GetRemoteHtmlCode(string Url)
string s = "";
MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
_xmlhttp.open("GET", Url, false, null, null);
_xmlhttp.send("");
if (_xmlhttp.readyState == 4)
s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody);
#region 儲存遠端檔案
* 函數名稱:RemoteSave
* 功能說明:儲存遠端檔案
* 參 數:Url:遠端url;Path:儲存到的路徑
* string s = "";
* string path =Server.MapPath("Html/");
* s = o.RemoteSave(url,path);
* o.Dispose();
* ******************************/
/// 儲存遠端檔案
/// <param name="Url">遠端url</param>
/// <param name="Path">儲存到的路徑</param>
public string RemoteSave(string Url, string Path)
Random ra = new Random();
string StringFileName = DateRndName(ra) + "." + GetFileExtends(Url);
string StringFilePath = Path + StringFileName;
if (System.IO.File.Exists(StringFilePath))
System.IO.File.Delete(StringFilePath);
System.IO.FileStream fs = new System.IO.FileStream(StringFilePath, System.IO.FileMode.CreateNew);
System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
w.Write((byte[])_xmlhttp.responseBody);
w.Close();
fs.Close();
else
throw new Exception(_xmlhttp.statusText);
return StringFileName;
#region 替換網頁中的換行和引号
* 函數名稱:ReplaceEnter
* 功能說明:替換網頁中的換行和引号
* 參 數:HtmlCode:html源代碼
* string Url = @"http://www.baidu.com/";
* strion HtmlCode = o.GetRemoteHtmlCode(Url);
* string s = o.ReplaceEnter(HtmlCode);
/// 替換網頁中的換行和引号
/// <param name="HtmlCode">HTML源代碼</param>
public string ReplaceEnter(string HtmlCode)
if (HtmlCode == null || HtmlCode == "")
s = "";
s = HtmlCode.Replace("\"", "");
s = s.Replace("\r\n", "");
#region 執行正則提取出值
* 函數名稱:GetRegValue
* 功能說明:執行正則提取出值
* string Reg="<title>.+?</title>";
* string GetValue=o.GetRegValue(Reg,HtmlCode)
* Response.Write(GetValue);
/// 執行正則提取出值
/// <param name="RegexString">正規表達式</param>
/// <param name="RemoteStr">HtmlCode源代碼</param>
public string GetRegValue(string RegexString, string RemoteStr)
string MatchVale = "";
Regex r = new Regex(RegexString);
Match m = r.Match(RemoteStr);
if (m.Success)
MatchVale = m.Value;
return MatchVale;
#region 替換HTML源代碼
* 函數名稱:RemoveHTML
* 功能說明:替換HTML源代碼
/// 替換HTML源代碼
/// <param name="HtmlCode">html源代碼</param>
public string RemoveHTML(string HtmlCode)
string MatchVale = HtmlCode;
foreach (Match s in Regex.Matches(HtmlCode, "<.+?>"))
MatchVale = MatchVale.Replace(s.Value, "");
#region 比對頁面的連結
* 函數名稱:GetHref
* 功能說明:比對頁面的連結
* string s = o.GetHref(HtmlCode);
/// 擷取頁面的連結正則
/// <param name="HtmlCode"></param>
public string GetHref(string HtmlCode)
string Reg = @"(h|H)(r|R)(e|E)(f|F) *= *('|"")?((\w|\\|\/|\.|:|-|_)+)('|""| *|>)?";
foreach (Match m in Regex.Matches(HtmlCode, Reg))
MatchVale += (m.Value).ToLower().Replace("href=", "").Trim() + "||";
#region 比對頁面的圖檔位址
* 函數名稱:GetImgSrc
* 功能說明:比對頁面的圖檔位址
* 參 數:HtmlCode:html源代碼;imgHttp:要補充的http.當比如:<img src="bb/x.gif">則要補充http://www.baidu.com/,當包含http資訊時,則可以為空
* string s = o.GetImgSrc(HtmlCode,"http://www.baidu.com/");
/// 比對頁面的圖檔位址
/// <param name="imgHttp">要補充的http://路徑資訊</param>
public string GetImgSrc(string HtmlCode, string imgHttp)
string Reg = @"<img.+?>";
MatchVale += GetImg((m.Value).ToLower().Trim(), imgHttp) + "||";
/// 比對<img src="" />中的圖檔路徑實際連結
/// <param name="ImgString"><img src="" />字元串</param>
public string GetImg(string ImgString, string imgHttp)
string Reg = @"src=.+\.(bmp|jpg|gif|png|)";
foreach (Match m in Regex.Matches(ImgString.ToLower(), Reg))
MatchVale += (m.Value).ToLower().Trim().Replace("src=", "");
return (imgHttp + MatchVale);
#region 替換通過正則擷取字元串所帶的正則首尾比對字元串
* string s = o.RegReplace(HtmlCode,"<title>","</title>");
/// 替換通過正則擷取字元串所帶的正則首尾比對字元串
/// <param name="RegValue">要替換的值</param>
/// <param name="regStart">正則比對的首字元串</param>
/// <param name="regEnd">正則比對的尾字元串</param>
public string RegReplace(string RegValue, string regStart, string regEnd)
string s = RegValue;
if (RegValue != "" && RegValue != null)
if (regStart != "" && regStart != null)
{
s = s.Replace(regStart, "");
}
if (regEnd != "" && regEnd != null)
s = s.Replace(regEnd, "");
}
作者:水木