在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 获取全国各地 近7天天气预报 示例源码

获取全国各地 近7天天气预报 示例源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.15M
  • 下载次数:48
  • 浏览次数:348
  • 发布时间:2018-04-30
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 天气预报 源码 天气 7

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace Weather
{
    class WeatherDetail
    {
        public PlaceModel province;
        public PlaceModel city;
        public PlaceModel district;
        public WeatherDetail(PlaceModel province,PlaceModel city,PlaceModel district)
        {
            this.province = province;
            this.city = city;
            this.district = district;
        }
        public string[] Day_1To7 { private set; get; }
        public string[] Day_7To15 { private set; get; }
        public string[] Info_1To7 { private set; get; }
        public string[] Info_7To15 { private set; get; }
        public string[] Temperature_1To7 { private set; get; }
        public string[] Temperature_7To15 { private set; get; }
        public string [] Wind_1To7 { private set; get; }
        public string [] Wind1_7To15 { private set; get; }
        public string[] Wind2_7To15 { private set; get; }
        public WeatherStatus[] WeatherStatus_1To7 { private set; get; }
        public WeatherStatus[] WeatherStatus_7To15 { private set; get; }
        public void HandleWeather()
        {
            using (WebClient webClient = new WebClient() { Encoding = Encoding.UTF8})
            {
                string url = $"http://www.weather.com.cn/weather/";
                if (city.ID == "00")
                {
                    url  = $"{province.ID}{district.ID}{city.ID}.shtml";
                }
                else if (district.ID.Length >2)
                {
                    url  = $"{district.ID}.shtml";
                }
                else 
                {
                    url  = $"{province.ID}{city.ID}{district.ID}.shtml";
                }
                 
                byte [] contentBytes = webClient.DownloadData(url);
                using (MemoryStream stream = new MemoryStream(contentBytes))
                using(StreamReader reader = new StreamReader(stream))
                {
                    string content = reader.ReadToEnd();
                    MatchCollection matches = Regex.Matches(content, "<ul class=\"t clearfix\">.*?</ul>", RegexOptions.Singleline);
                    if(matches.Count > 0)
                    {
                        matches = Regex.Matches(matches[0].Value, "<li class=\"sky skyid.*?</li>",RegexOptions.Singleline);
                        if(matches.Count == 7)
                        {
                            List<string> dayList = new List<string>();
                            List<string> infoList = new List<string>();
                            List<string> tempList = new List<string>();
                            List<string> windList = new List<string>();
                            List<WeatherStatus> statusList = new List<WeatherStatus>();
                            MatchCollection matches_temp;
                            foreach (Match match in matches)
                            {
                                //日期:31日(今日)
                                matches_temp = Regex.Matches(match.Value, "<h1>.*?</h1>");
                                if (matches_temp.Count > 0) dayList.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                else dayList.Add("");
                                //天气信息:晴
                                matches_temp = Regex.Matches(match.Value, "<p.*class=\"wea\".*?</p>");
                                if (matches_temp.Count > 0)
                                {
                                    infoList.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                    WeatherStatus status = GetWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value));
                                    if(status == WeatherStatus.Weizhi)
                                    {
                                        status = GetUnkownWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value));
                                    }
                                    statusList.Add(status);
                                }
                                else
                                {
                                    dayList.Add("");
                                    statusList.Add(WeatherStatus.Weizhi);
                                }
                                //温度信息:4/-1℃
                                //风力信息:3-4级转3级
                                matches_temp = Regex.Matches(match.Value, "<span>.*?</span>");
                                string temperature = "";
                                string wind = "";
                                if (matches_temp.Count > 0)
                                {
                                    temperature = RemoveAngleBrackets(matches_temp[0].Value) "℃";
                                }
                                matches_temp = Regex.Matches(match.Value, "<i>.*?</i>");
                                if (matches_temp.Count >= 1)
                                {
                                    temperature = string.IsNullOrEmpty(temperature) ? RemoveAngleBrackets(matches_temp[0].Value) : $"{temperature}/{RemoveAngleBrackets(matches_temp[0].Value)}";
                                }
                                if(matches_temp.Count >= 2)
                                {
                                    wind = RemoveAngleBrackets(matches_temp[1].Value);
                                }
                                tempList.Add(temperature);
                                windList.Add(wind);
                            }
                            this.Day_1To7 = dayList.ToArray();
                            this.Info_1To7 = infoList.ToArray();
                            this.Temperature_1To7 = tempList.ToArray();
                            this.Wind_1To7 = windList.ToArray();
                            this.WeatherStatus_1To7 = statusList.ToArray();
                        }
                    }
                }

                //More
                url = $"http://www.weather.com.cn/weather15d/";
                if (city.ID == "00")
                {
                    url  = $"{province.ID}{district.ID}{city.ID}.shtml";
                }
                else if (district.ID.Length > 2)
                {
                    url  = $"{district.ID}.shtml";
                }
                else
                {
                    url  = $"{province.ID}{city.ID}{district.ID}.shtml";
                }

                contentBytes = webClient.DownloadData(url);
                using (MemoryStream stream = new MemoryStream(contentBytes))
                using (StreamReader reader = new StreamReader(stream))
                //----
                {
                    string content = reader.ReadToEnd();
                    MatchCollection matches = Regex.Matches(content, "<ul class=\"t clearfix\">.*?</ul>", RegexOptions.Singleline);
                    if (matches.Count > 0)
                    {
                        matches = Regex.Matches(matches[0].Value, "<li.*?</li>", RegexOptions.Singleline);
                        if (matches.Count == 8)
                        {
                            List<string> dayList = new List<string>();
                            List<string> infoList = new List<string>();
                            List<string> tempList = new List<string>();
                            List<string> wind1List = new List<string>();
                            List<string> wind2List = new List<string>();
                            List<WeatherStatus> statusList = new List<WeatherStatus>();
                            MatchCollection matches_temp;
                            foreach (Match match in matches)
                            {
                                //日期:31日(今日)
                                matches_temp = Regex.Matches(match.Value, "<span class=\"time\">.*?</span>");
                                if (matches_temp.Count > 0) dayList.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                else dayList.Add("");
                                //天气信息:晴
                                matches_temp = Regex.Matches(match.Value, "<span class=\"wea\">.*?</span>");
                                if (matches_temp.Count > 0)
                                {
                                    infoList.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                    WeatherStatus status = GetWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value));
                                    if (status == WeatherStatus.Weizhi)
                                    {
                                        status = GetUnkownWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value));
                                    }
                                    statusList.Add(status);
                                }
                                else
                                {
                                    dayList.Add("");
                                    statusList.Add(WeatherStatus.Weizhi);
                                }
                                //温度信息:4/-1℃
                                //风力信息:3-4级转3级
                                matches_temp = Regex.Matches(match.Value, "<span class=\"tem\".*?</span>");
                                if (matches_temp.Count > 0) tempList.Add(RemoveAngleBrackets(RemoveAngleBrackets(matches_temp[0].Value)));
                                else tempList.Add("");

                                matches_temp = Regex.Matches(match.Value, "<span class=\"wind\">.*?</span>");
                                if (matches_temp.Count > 0) wind1List.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                else wind1List.Add("");

                                matches_temp = Regex.Matches(match.Value, "<span class=\"wind1\">.*?</span>");
                                if (matches_temp.Count > 0) wind2List.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                else wind2List.Add("");
                            }
                            this.Day_7To15 = dayList.ToArray();
                            this.Info_7To15 = infoList.ToArray();
                            this.Temperature_7To15 = tempList.ToArray();
                            this.Wind1_7To15 = wind1List.ToArray();
                            this.Wind2_7To15 = wind2List.ToArray();
                            this.WeatherStatus_7To15 = statusList.ToArray();
                        }
                    }
                }
                //----

            }
        }

        private string RemoveAngleBrackets(string input)
        {
            int firstStartIndex = input.IndexOf('<');
            int firstEndIndex = input.IndexOf('>');
            string result = input.Remove(firstStartIndex, firstEndIndex- firstStartIndex   1);
            firstStartIndex = result.LastIndexOf('<');
            firstEndIndex = result.LastIndexOf('>');
            result = result.Remove(firstStartIndex, firstEndIndex- firstStartIndex   1);
            return result;
        }

        private WeatherStatus GetWeatherStatus(string weatherInfo)
        {
            switch(weatherInfo)
            {
                case "晴": return WeatherStatus.Qing;
                case "多云": return WeatherStatus.Duoyun;
                case "阴": return WeatherStatus.Yin;
                case "阵雨": return WeatherStatus.Zhenyu;
                case "雷阵雨": return WeatherStatus.Leizhenyu;
                case "雷阵雨并伴有冰雹": return WeatherStatus.LeizhenyuBingpao;
                case "雨夹雪": return WeatherStatus.Yujiaxue;
                case "小雨": return WeatherStatus.Xiaoyu;
                case "中雨": return WeatherStatus.Zhongyu;
                case "大雨": return WeatherStatus.Dayu;
                case "暴雨": return WeatherStatus.Baoyu;
                case "大暴雨": return WeatherStatus.Dabaoyu;
                case "特大暴雨": return WeatherStatus.Tedabaoyu;
                case "阵雪": return WeatherStatus.Zhenxue;
                case "小雪": return WeatherStatus.Xiaoyu;
                case "中雪": return WeatherStatus.Zhongxue;
                case "大雪": return WeatherStatus.Daxue;
                case "暴雪": return WeatherStatus.Baoxue;
                case "雾": return WeatherStatus.Wu;
                case "冻雨": return WeatherStatus.Dongyu;
                case "沙尘暴": return WeatherStatus.Shachenbao;
                case "小雨转中雨": return WeatherStatus.XiaoyuZhuangZhongyu;
                case "中雨转大雨": return WeatherStatus.ZhongyuZhuangDayu;
                case "大雨转暴雨": return WeatherStatus.DayuZhuangBaoyu;
                case "暴雨转大暴雨": return WeatherStatus.BaoyuZhuangDabaoyu;
                case "大暴雨转特大暴雨": return WeatherStatus.DabaoyuZhuangTedabaoyu;
                case "小雪转中雪": return WeatherStatus.XiaoxueZhuangZhongxue;
                case "中雪转大雪": return WeatherStatus.ZhongxueZhuangDaxue;
                case "大雪转暴雪": return WeatherStatus.DaxueZhuangBaoxue;
                case "浮尘": return WeatherStatus.Fuchen;
                case "扬沙": return WeatherStatus.Yasha;
                case "强沙尘暴": return WeatherStatus.Qiangshachenbao;
                case "小到中雨": return WeatherStatus.Xiaodaozhongyu;
                case "中到大雨": return WeatherStatus.Zhongdaodayu;
                case "大到暴雨": return WeatherStatus.Dadaobaoyu;
                case "小到中雪": return WeatherStatus.Xiaodaozhongxue;
                case "中到大雪": return WeatherStatus.Zhongdaodaxue;
                case "大到暴雪": return WeatherStatus.Dadaobaoxue;
                case "小阵雨": return WeatherStatus.Xiaozhenyu;
                case "阴天": return WeatherStatus.Yintian;
                case "霾": return WeatherStatus.Mai;
                case "雾霾": return WeatherStatus.Wumai;
                default:return WeatherStatus.Weizhi;
            }
        }

        private WeatherStatus GetUnkownWeatherStatus(string weatherInfo)
        {
            string result = weatherInfo;

            int index = 0;
            if (result.Contains("间"))
            {
                index = result.IndexOf("间");
                result = result.Remove(index, result.Length - index);
            }
            if(result.Contains("转"))
            {
                index = result.IndexOf("转");
                result = result.Remove(index, result.Length - index);
            }
            
            if (result.Contains("雨")) return WeatherStatus.Xiaoyu;
            if (result.Contains("雪")) return WeatherStatus.Xiaoxue;
            if (result.Contains("雾")) return WeatherStatus.Wu;
            if (result.Contains("霾")) return WeatherStatus.Mai;
            
            return GetWeatherStatus(result);
        }
    }
}

实例下载地址

获取全国各地 近7天天气预报 示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警