zoukankan      html  css  js  c++  java
  • 主攻ASP.NET.3.5.MVC3.0架构之重生:自定义农历日期天气格式Helper

    效果图

           #region 获取IP地址
            /// <summary>
            /// 获取IP地址
            /// </summary>
            /// <returns></returns>
            public static string GetIpAddress()
            {
                //获取IP地址
                string IpString = StringHelper.GetHtmlContentByUrl("http://iframe.ip138.com/ic.asp", "gb2312");
                string regcityString = @"\[(.*)\]";
                string IpAddress = StringHelper.Reg(regcityString, IpString, 1);
                return IpAddress;
            }
            #endregion


            #region 获取当天星期农历
            /// <summary>
            /// 获取当天星期农历
            /// </summary>
            /// <returns></returns>
            public static string GetDataString()
            {
                string dataString = StringHelper.GetHtmlContentByUrl("http://www.nongli114.com/", "gb2312");

                //获取当天星期几
                string regString = @"</b> ([^)]*?)</td>";
                string data = StringHelper.Reg(regString, dataString, 1);


                //获取当天农历
                string regAString = @"<div id=""span_select_lunar_month"">([^)]*?)</div>";
                string dataA = StringHelper.Reg(regAString, dataString, 1);

                string regBString = @"<div id=""span_select_lunar_day"">([^)]*?)</div>";
                string dataB = StringHelper.Reg(regBString, dataString, 1);
                string dataForString = data+" "+"农历" + dataA + "月" + dataB + "日";
                return dataForString;
            }
            #endregion

            #region 获取地方天气
            /// <summary>
            /// 获取地方天气
            /// </summary>
            /// <returns></returns>
            public static string GetWeatherString()
            {
                //获取地方天气

                //string weatherAString = StringHelper.GetHtmlContentByUrl("http://61.4.185.48:81/g/","gb2312");
                //string weatherReg = @"id=([^)]*?);if";
                //string weatherID = StringHelper.Reg(weatherReg, weatherAString, 1);
                string weatherID = "101200101";//获取武汉天气
                string weatherBString = StringHelper.GetHtmlContentByUrl("http://m.weather.com.cn/data/" + weatherID + ".html", "utf-8");
                string cityReg = @"""city"":""([^)]*?)""";
                string cityString = StringHelper.Reg(cityReg, weatherBString, 1);

                string weather1Reg = @"""weather1"":""([^)]*?)""";
                string weather1String = StringHelper.Reg(weather1Reg, weatherBString, 1);


                string temp1Reg = @"""temp1"":""([^)]*?)""";
                string temp1String = StringHelper.Reg(temp1Reg, weatherBString, 1);
                string weatherString = cityString + weather1String + temp1String;
                return weatherString;
            }
            #endregion
           

    补代码

     #region 过滤匹配字符串
            /// <summary>
            /// 过滤匹配字符串
            /// </summary>
            /// <param name="regexstr">正则表达式</param>
            /// <param name="codestr">HTML/CODE/STRING</param>
            /// <returns></returns>
            public static string Reg(string regexstr, string codestr, int index)
            {
                Regex VIEWSTATERegex = new Regex(regexstr);
                MatchCollection VIEWSTATEMatchResult = VIEWSTATERegex.Matches(codestr);
                string regstr = "";
                foreach (Match vmr in VIEWSTATEMatchResult)
                {
                    regstr = vmr.Groups[index].Value.ToString();
                }
                return regstr;
            }
            #endregion
            /// <summary>  
    /// 根据url获取网站HTML内容 /// </summary> /// <param name="url">网址</param> /// <param name="url">编码</param> /// <returns>获取网站HTML内容</returns> public static string GetHtmlContentByUrl(string url, string encode) { var htmlContent = string.Empty; var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Timeout = 10000000; var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); var stream = httpWebResponse.GetResponseStream(); if (stream != null) { var streamReader = new StreamReader(stream, Encoding.GetEncoding(encode)); htmlContent = streamReader.ReadToEnd(); streamReader.Close(); streamReader.Dispose(); stream.Close(); stream.Dispose(); } httpWebResponse.Close(); return htmlContent; }
  • 相关阅读:
    [学习笔记]设计模式之Bridge
    整数划分问题 动态规划
    powershell 发邮件
    python 对象序列化并压缩
    python的序列化与反序列化(例子:dict保存成文件,文件读取成dict)
    ACM-ICPC 2018 world final A题 Catch the Plane
    AlphaPose论文笔记《RMPE: Regional Multi-person Pose Estimation》
    《DensePose: Dense Human Pose Estimation In The Wild》阅读笔记
    [转]tensorflow 中的卷积conv2d的padding 到底要padding多少
    OpenPose论文笔记《Realtime Multi-Person 2D Human Pose Estimation using Part Affinity Fields》
  • 原文地址:https://www.cnblogs.com/cube/p/2802789.html
Copyright © 2011-2022 走看看