Asp.net 資料擷取基類(遠程抓取,分解,儲存,匹配)

來源:互聯網
上載者:User

Asp.net 資料擷取基類(遠程抓取,分解,儲存,匹配)
詳細參考地址:http://www.opent.cn/article.asp?id=5

#region 日期隨機函數
        /**********************************
         * 函數名稱:DateRndName
         * 功能說明:日期隨機函數
         * 參    數:ra:隨機數
         * 調用樣本:
         *          GetRemoteObj o = new GetRemoteObj();
         *          Random ra = new Random();
         *          string s = o.DateRndName(ra);
         *          Response.Write(s);
         *          o.Dispose();
         * ********************************/
        /// <summary>
        /// 日期隨機函數
        /// </summary>
        /// <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;
        }
        #endregion

        #region 取得檔案尾碼
        /**********************************
         * 函數名稱:GetFileExtends
         * 功能說明:取得檔案尾碼
         * 參    數:filename:檔案名稱
         * 調用樣本:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string url = @"http://www.baidu.com/img/logo.gif";
         *          string s = o.GetFileExtends(url);
         *          Response.Write(s);
         *          o.Dispose();
         * ********************************/
        /// <summary>
        /// 取得檔案尾碼
        /// </summary>
        /// <param name="filename">檔案名稱</param>
        /// <returns></returns>
        public string GetFileExtends(string filename)
        {
            string ext = null;
            if (filename.IndexOf('.') > 0)
            {
                string[] fs = filename.Split('.');
                ext = fs[fs.Length - 1];
            }
            return ext;
        }
        #endregion

        #region 擷取遠程檔案原始碼
        /**********************************
         * 函數名稱:GetRemoteHtmlCode
         * 功能說明:擷取遠程檔案原始碼
         * 參    數:Url:遠程url
         * 調用樣本:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string url = @"http://www.baidu.com";
         *          string s = o.GetRemoteHtmlCode(url);
         *          Response.Write(s);
         *          o.Dispose();
         * ********************************/
        /// <summary>
        /// 擷取遠程檔案原始碼
        /// </summary>
        /// <param name="url">遠程url</param>
        /// <returns></returns>
        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);
            }
            return s;
        }

        #endregion
取得頁面HTML代碼的例子上面已經給出不少了
下面的代碼是使用Regex取得HTML中內容的代碼
Regex regex1 = new Regex(this.NameKey, RegexOptions.Singleline | RegexOptions.IgnoreCase );
MatchCollection collection1 = regex1.Matches(this.ArtHtml);
if (collection1.Count>0)
{
result=collection1[0].Result("${title}");
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.