zoukankan      html  css  js  c++  java
  • 下载网页通用类

        /// <summary>
        /// 根据网页地址去下载网页,包括从网页中获取自己所需要的内容
        /// </summary>
        public class WebWrapper
        {
            public WebWrapper() { }
            public WebWrapper(string link) { Link = link; }
    
            #region Download html
    
            public string Link { get; set; }
            public string DownLoadHtml(string link)
            {
                string htmlString = string.Empty;
                try
                {
                    WebRequest webRequest = System.Net.WebRequest.Create(link);
    
                    webRequest.Method = "GET";
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                    using (StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream()))
                    {
                        htmlString = sr.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(string.Format("Error Downloading CurrencyRate: {0}", ex.Message));
                }
                return htmlString;
            }
            public string DownLoadHtml()
            {
                if (string.IsNullOrEmpty(Link))
                    throw new ApplicationException("Please input web address");
                return DownLoadHtml(Link);
            }
    
            #endregion
    
            #region Get matched content
    
            public string GetContent(string html, string regexExpress)
            {
                Regex re = new Regex(regexExpress);
                MatchCollection ms = re.Matches(html);
                if (ms == null || ms.Count == 0)
                    throw new ApplicationException("Can not find matched content");
    
                return ms[0].ToString();
            }
    
            public string GetContent(string regexExpress)
            {
                string html = DownLoadHtml();
                return GetContent(html, regexExpress);
            }
    
            #endregion
    
            #region Regex Properties
    
            public string BeginConstruction = "(?<=({0}))";
            public string EndConstruction = "(?=({0}))";
            public string RegexBeginExpression(string begin) { return string.Format(BeginConstruction, begin); }
            public string RegexEndExpression(string end) { return string.Format(EndConstruction, end); }
            public string RegexAnyExpression { get { return "[.\s\S]*?"; } }
    
            #endregion
        }
    

      

  • 相关阅读:
    游戏开发系统功能(9)
    游戏开发创建游戏世界(8)
    游戏开发沟通和开会技巧(7)
    游戏开发策划工作(6)
    游戏开发了解测试工作(4)
    Django:学习笔记(9)——用户身份认证
    Django:学习笔记(8)——视图
    Django:学习笔记(7)——模型进阶
    Django:学习笔记(6)——模型
    Django:学习笔记(5)——会话
  • 原文地址:https://www.cnblogs.com/EasyInvoice/p/3808844.html
Copyright © 2011-2022 走看看