明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 321万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

抓取国家气象局天气预报3天!!

Posted on   且行且思  阅读(1919)  评论(0编辑  收藏  举报

/// <summary>
        /// 国家气象局天气预报 3天
        /// </summary>
        /// <returns></returns>
        public static XmlDataDocument GetCMAWeather()
        {
            //http://www.cma.gov.cn/tqyb/
            //http://www.cma.gov.cn/tqyb/weatherdetail/57957.html

            XmlDataDocument objXml = new XmlDataDocument();
            objXml.LoadXml("<root />");
            //抓取所有城市列表
            string content = GetContent("http://www.cma.gov.cn/tqyb/", "gb2312");

            //抓到内容后,开始分析数据
            Regex regex;
            Match mc;
            XmlElement objXmlCityList = objXml.CreateElement("citylist");

            string partten = "\"(?<citycode>[0-9]{5,})\"";
            regex = new Regex(partten, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            objXmlCityList.SetAttribute("vdatetime", DateTime.Now.ToShortDateString());
            Yesun.Edzh.BLL.Log.LogHelperService.WriteError("开始从中国气象局抓取天气预报");
            //"50774"
            int i = 1;
            for (mc = regex.Match(content), i = 1; mc.Success; mc = mc.NextMatch(), i++)
            {
                try
                {
                    //Yesun.Edzh.BLL.Log.LogHelperService.WriteError(i + "、" + mc.Groups["citycode"].Value.Trim());
                    //根据城市代码组合新的地址,抓取城市天气预报
                    if(Convert.ToInt32(mc.Groups["citycode"].Value) > 10000)
                    {
                        string cityUrl = "http://www.cma.gov.cn/tqyb/weatherdetail/" + mc.Groups["citycode"].Value + ".html";
                        //Yesun.Edzh.BLL.Log.LogHelperService.WriteError("CityUrl : " + cityUrl);
                        content = GetContent(cityUrl, "gb2312");
                        //真正开始分析天气预报数据
                        string parttenCity = "3天预报&nbsp;&nbsp;&nbsp;(?<cityname>.*)</div>";
                        Match mcCity;
                        regex = new Regex(parttenCity, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        int j = 1;
                        for (mcCity = regex.Match(content), j = 1; mcCity.Success; mcCity = mcCity.NextMatch(), j++)
                        {
                            try
                            {
                                XmlElement objXmlElementCity = objXml.CreateElement("city");
                                Yesun.Edzh.BLL.Log.LogHelperService.WriteError("城市 : " + mcCity.Groups["cityname"].Value);
                                objXmlElementCity.SetAttribute("cityname", mcCity.Groups["cityname"].Value.Trim());
                                //读取天气信息
                                //日期
                                string parttenTmp = "<td width=\"(138|137)\" class=\"b-cn\">(?<item1>[^<]+)月(?<item2>[^<]+)日</td>";
                                Match mcTmp;
                                int k = 1;
                                regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                                for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
                                {
                                    if (k > 3)
                                        break;
                                    //Yesun.Edzh.BLL.Log.LogHelperService.WriteError("日期 : " + mcTmp.Groups["item1"].Value);
                                    XmlElement itemElement = objXml.CreateElement("day");
                                    itemElement.SetAttribute("vdatetime",DateTime.Now.Year+"-"+mcTmp.Groups["item1"].Value+"-"+mcTmp.Groups["item2"].Value);
                                    objXmlElementCity.AppendChild(itemElement);
                                }
                                //天气
                                parttenTmp = "<td width=\"75\" valign=\"middle\" class=\"red-cn\">(?<item1>[^<]+)</td>";
                                k = 1;
                                regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                                for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
                                {
                                    //Yesun.Edzh.BLL.Log.LogHelperService.WriteError("天气 : " + mcTmp.Groups["item1"].Value);
                                    //加入天气
                                    XmlElement itemElement = (XmlElement)objXmlElementCity.SelectNodes("day")[k-1];
                                    if (itemElement != null)
                                        itemElement.SetAttribute("weather", mcTmp.Groups["item1"].Value);
                                }
                                //气温
                                parttenTmp = "class=\"b-cn\">(?<item1>[^<]+)℃</td>";
                                k = 1;
                                regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                                for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
                                {
                                    if (k > 3)
                                        break;
                                    //Yesun.Edzh.BLL.Log.LogHelperService.WriteError("气温 : " + mcTmp.Groups["item1"].Value);
                                    //加入气温
                                    XmlElement itemElement = (XmlElement)objXmlElementCity.SelectNodes("day")[k-1];
                                    if (itemElement!=null)
                                        itemElement.SetAttribute("temperature", mcTmp.Groups["item1"].Value + "℃");
                                }
                                //风力/风向
                                parttenTmp = "class=\"b-cn\">(?<item1>[^<]+)风</td>";
                                k = 1;
                                regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                                for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
                                {
                                    if (k > 3)
                                        break;
                                    //Yesun.Edzh.BLL.Log.LogHelperService.WriteError("风力/风向 : " + mcTmp.Groups["item1"].Value+"风");
                                    //加入风力/风向
                                    XmlElement itemElement = (XmlElement)objXmlElementCity.SelectNodes("day")[k-1];
                                    if (itemElement != null)
                                        itemElement.SetAttribute("wind", mcTmp.Groups["item1"].Value+"风");
                                }
                                //指数查询
                                parttenTmp = "<td width=\"67\" class=\"b-cn\">(?<item1>[^<]+)</td>(\\s*)<td valign=\"middle\" class=\"cn\"><a href=\"(?<xx>[^>]+)\" title=\"(?<item2>[^>]+)\">(?<title>[^<]+)</a></td>";
                                k = 1;
                                string comment = "";
                                regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                                for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
                                {
                                    //Yesun.Edzh.BLL.Log.LogHelperService.WriteError(mcTmp.Groups["item1"].Value +" : "+mcTmp.Groups["item2"].Value);
                                    comment += mcTmp.Groups["item1"].Value + " : " + mcTmp.Groups["item2"].Value + "<br/>";
                                }
                                XmlElement objXmlComment = objXml.CreateElement("comment");
                                XmlCDataSection objCdata = objXml.CreateCDataSection(comment);
                                objXmlComment.AppendChild((XmlNode)objCdata);
                                objXmlElementCity.AppendChild(objXmlComment);

                                objXmlCityList.AppendChild(objXmlElementCity);
                            }
                            catch (Exception ex)
                            {
                                Yesun.Edzh.BLL.Log.LogHelperService.WriteError(ex.Message);
                            }
                        }
                        objXml.DocumentElement.AppendChild(objXmlCityList);
                    }

                }
                catch (Exception ex)
                {
                    Yesun.Edzh.BLL.Log.LogHelperService.WriteError(ex.Message);
                }
            }

            Yesun.Edzh.BLL.Log.LogHelperService.WriteError("成功从中国气象局抓取天气预报!");

            return objXml;
        }

        /// <summary>
        /// 抓取页面接口
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private static string GetContent(string url, string encoding)
        {
            string str = "";
            WebClient client = new WebClient();
            client.Headers.Add("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            client.Headers.Add("Accept-Language", "zh-cn");
            client.Headers.Add("UA-CPU", "x86");
            client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
            try
            {
                byte[] buffer = client.DownloadData(url);
                if (encoding == "utf-8")
                {
                    str = System.Text.Encoding.GetEncoding("utf-8").GetString(buffer, 0, buffer.Length);
                }
                else
                {
                    str = System.Text.Encoding.GetEncoding("gb2312").GetString(buffer, 0, buffer.Length);
                }
            }
            catch (Exception ex)
            {
                Yesun.Edzh.BLL.Log.LogHelperService.WriteError(ex.Message);
            }
            return str;
        }

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示