Ejemplo n.º 1
0
        public Dictionary <string, string> GetTodayWeather2(string weatherUrl, string weatherImagePath)
        {
            var web = new HtmlWeb();
            var doc = web.Load(weatherUrl);

            HtmlNode weather_info = doc.QuerySelector(".weather_info");

            HtmlNode week = weather_info.QuerySelector(".week");

            string[] date = week.InnerText.Trim().Split(" ", StringSplitOptions.RemoveEmptyEntries);

            HtmlNode weather = weather_info.QuerySelector(".weather");
            Dictionary <string, string> weatherDict = new Dictionary <string, string>();
            HtmlNode name   = weather_info.QuerySelector(".name");
            string   region = name.Descendants("h2").FirstOrDefault().InnerText.Trim();

            weatherDict.Add("region", region);
            weatherDict.Add("day", date[0]);
            weatherDict.Add("week", date[1]);
            weatherDict.Add("lunarDay", date[2]);
            string img_url = weather.Descendants("img").Select(x => x.Attributes["src"].Value).FirstOrDefault();

            if (!img_url.Contains("http:"))
            {
                if (img_url.StartsWith("//"))
                {
                    img_url = "http:" + img_url;
                }
                else if (img_url.StartsWith("www"))
                {
                    img_url = "http://" + img_url;
                }
            }
            weatherDict.Add("image", img_url);
            var    tempAndDescribe = weather.Descendants("b");
            string now_temperature = "";
            string temp_describe   = "";

            if (tempAndDescribe.Count() > 0)
            {
                now_temperature = tempAndDescribe.FirstOrDefault().InnerText.Trim();
                temp_describe   = tempAndDescribe.LastOrDefault().InnerText.Trim();
            }
            string highAndlow_temp = weather.Descendants("span").Count() > 0 ? weather.Descendants("span").FirstOrDefault().InnerHtml.Split(new string[] { "</b>" }, StringSplitOptions.RemoveEmptyEntries)[1] : "";

            string[] high_low = highAndlow_temp.Replace("℃", "").Replace(" ", "").Split('~');
            string   low      = "";
            string   high     = "";

            if (high_low.Length == 2)
            {
                low  = high_low[0];
                high = high_low[1];
            }

            weatherDict.Add("nowTemperature", now_temperature);
            weatherDict.Add("lowTemperature", low);
            weatherDict.Add("highTemperature", high);
            weatherDict.Add("describe", temp_describe);
            string[] td_keys = temp_describe.Split('转');
            string   td_key  = td_keys.Count() > 1 ? td_keys[1] : td_keys[0];

            if (describe_code.ContainsKey(td_key))
            {
                string code = describe_code[td_key];
                weatherDict.Add("code", code);
                weatherDict.Add("whiteImage", "/Images/weather_icon_white/" + code + ".png");
                weatherDict.Add("blackImage", "/Images/weather_icon/" + code + ".png");
            }
            else
            {
                weatherDict.Add("code", "999");
                weatherDict.Add("whiteImage", "/Images/weather_icon_white/999.png");
                weatherDict.Add("blackImage", "/Images/weather_icon/999.png");
            }

            weatherDict.Add("aqiValue", "0");
            weatherDict.Add("aqiLevel", "0");
            HtmlNode kongqi      = weather_info.QuerySelector(".kongqi");
            string   aqiDescribe = kongqi.Descendants("h5").Count() > 0 ? kongqi.Descendants("h5").FirstOrDefault().InnerText.Trim().Split(':', ':')[1].Trim() : "";

            weatherDict.Add("aqiDescribe", aqiDescribe);
            string pm = kongqi.Descendants("h6").Count() > 0 ? kongqi.Descendants("h6").FirstOrDefault().InnerText.Trim().Split(':', ':')[1].Trim() : "";

            weatherDict.Add("particulateMatter", pm);
            var kongqi_descendants = kongqi.Descendants("span").ToList();

            string[] sun     = kongqi_descendants.FirstOrDefault().InnerHtml.Split(new string[] { "<br/>", "<br>" }, StringSplitOptions.RemoveEmptyEntries);
            string   sunrise = kongqi_descendants.Count > 0 ? sun[0].Trim() : "";

            weatherDict.Add("sunrise", sunrise);
            string sunset = kongqi_descendants.Count > 0 ? sun[1].Trim() : "";

            weatherDict.Add("sunset", sunset);

            HtmlNode shidu             = weather_info.QuerySelector(".shidu");
            var      shidu_descendants = shidu.Descendants("b").ToList();
            int      shidu_count       = shidu_descendants.Count;
            string   humidity          = shidu_count > 0 ? shidu_descendants[0].InnerText.Trim().Split(':', ':')[1].Trim() : "";

            weatherDict.Add("humidity", humidity);
            string wind = shidu_count > 0 ? shidu_descendants[1].InnerText.Trim().Split(':', ':')[1].Trim() : "";

            weatherDict.Add("wind", wind);
            string ultravioletRays = shidu_count > 0 ? shidu_descendants[2].InnerText.Trim().Split(':', ':')[1].Trim() : "";

            weatherDict.Add("ultravioletRays", ultravioletRays);

            try
            {
                if (client == null)
                {
                    client = new HttpClient();
                }
                byte[] imageArr = client.GetByteArrayAsync(img_url).Result;
                File.WriteAllBytesAsync(weatherImagePath + temp_describe + ".png", imageArr);
                if (weatherDict.ContainsKey("image"))
                {
                    weatherDict["image"] = "/Images/" + temp_describe + ".png";
                }
            }
            catch (Exception e) { }

            return(weatherDict);
        }