正則抓取SINA天氣預報資料!!!

來源:互聯網
上載者:User

/// <summary>
        /// SINA天氣預報 1天
        /// </summary>
        /// <returns></returns>
        public static XmlDataDocument GetSinaWeather()
        {
            //http://weather.news.sina.com.cn/images/figureWeather/map/northEast.html
            //http://weather.news.sina.com.cn/images/figureWeather/map/eastOfChina.html
            //http://weather.news.sina.com.cn/images/figureWeather/map/northOfChina.html
            //http://weather.news.sina.com.cn/images/figureWeather/map/southOfChina.html
            //http://weather.news.sina.com.cn/images/figureWeather/map/southWest.html
            //http://weather.news.sina.com.cn/images/figureWeather/map/northWest.html

            XmlDataDocument objXml = new XmlDataDocument();
            objXml.LoadXml("<root />");
            //東北
            string content = GetContent("http://weather.news.sina.com.cn/images/figureWeather/map/northEast.html","gb2312");

            //抓到內容後,開始分析資料
            Regex regex;
            Match mc;
            XmlElement objXmlCityList = objXml.CreateElement("citylist");

            string partten = "城市:(?<city>[^<]+) <br> 天氣:(?<weather>[^<]+)<br> 溫度:(?<temperature>[^<]+)<br>風向:(?<windway>[^<]+)<br>風力:(?<wind>[^<]+) <br>";
            regex = new Regex(partten, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            objXmlCityList.SetAttribute("vdatetime", DateTime.Now.ToShortDateString());
            //城市:哈爾濱市 <br> 天氣:多雲轉晴<br> 溫度:-10 ℃~-1 ℃<br>風向: 西風<br>風力:小於3級<br>
   int i= 1;
            for (mc = regex.Match(content), i = 1; mc.Success; mc = mc.NextMatch(), i++)
            {
                try
                {
                    Yesun.Edzh.BLL.Log.LogHelper.WriteError(i + "、" + mc.Groups["city"].Value.Trim());
                    XmlElement objXmlElementCity = objXml.CreateElement("city");
                    objXmlElementCity.SetAttribute("orders", "1");
                    objXmlElementCity.SetAttribute("city", mc.Groups["city"].Value.Trim());
                    objXmlElementCity.SetAttribute("weather", mc.Groups["weather"].Value.Trim());
                    objXmlElementCity.SetAttribute("temperature", mc.Groups["temperature"].Value.Trim());
                    objXmlElementCity.SetAttribute("windway", mc.Groups["windway"].Value.Trim());
                    objXmlElementCity.SetAttribute("wind", mc.Groups["wind"].Value.Trim());
                    objXmlCityList.AppendChild(objXmlElementCity);
                }
                catch(Exception ex)
                {
                    Yesun.Edzh.BLL.Log.LogHelper.WriteError(ex.Message);
                }
            }

            //東
            content = GetContent("http://weather.news.sina.com.cn/images/figureWeather/map/eastOfChina.html", "gb2312");
            for (mc = regex.Match(content), i = 1; mc.Success; mc = mc.NextMatch(), i++)
            {
                try
                {
                    XmlElement objXmlElementCity = objXml.CreateElement("city");
                    objXmlElementCity.SetAttribute("orders", "2");
                    objXmlElementCity.SetAttribute("city", mc.Groups["city"].Value.Trim());
                    objXmlElementCity.SetAttribute("weather", mc.Groups["weather"].Value.Trim());
                    objXmlElementCity.SetAttribute("temperature", mc.Groups["temperature"].Value.Trim());
                    objXmlElementCity.SetAttribute("windway", mc.Groups["windway"].Value.Trim());
                    objXmlElementCity.SetAttribute("wind", mc.Groups["wind"].Value.Trim());
                    objXmlCityList.AppendChild(objXmlElementCity);
                }
                catch (Exception ex)
                {
                    Yesun.Edzh.BLL.Log.LogHelper.WriteError(ex.Message);
                }
            }
            //北
            content = GetContent("http://weather.news.sina.com.cn/images/figureWeather/map/northOfChina.html", "gb2312");
            for (mc = regex.Match(content), i = 1; mc.Success; mc = mc.NextMatch(), i++)
            {
                try
                {
                    Yesun.Edzh.BLL.Log.LogHelper.WriteError(i + "、" + mc.Groups["city"].Value.Trim());
                    XmlElement objXmlElementCity = objXml.CreateElement("city");
                    objXmlElementCity.SetAttribute("orders", "3");
                    objXmlElementCity.SetAttribute("city", mc.Groups["city"].Value.Trim());
                    objXmlElementCity.SetAttribute("weather", mc.Groups["weather"].Value.Trim());
                    objXmlElementCity.SetAttribute("temperature", mc.Groups["temperature"].Value.Trim());
                    objXmlElementCity.SetAttribute("windway", mc.Groups["windway"].Value.Trim());
                    objXmlElementCity.SetAttribute("wind", mc.Groups["wind"].Value.Trim());
                    objXmlCityList.AppendChild(objXmlElementCity);
                }
                catch (Exception ex)
                {
                    Yesun.Edzh.BLL.Log.LogHelper.WriteError(ex.Message);
                }
            }

            //南
            content = GetContent("http://weather.news.sina.com.cn/images/figureWeather/map/southOfChina.html", "gb2312");
            for (mc = regex.Match(content), i = 1; mc.Success; mc = mc.NextMatch(), i++)
            {
                try
                {
                    XmlElement objXmlElementCity = objXml.CreateElement("city");
                    objXmlElementCity.SetAttribute("orders", "4");
                    objXmlElementCity.SetAttribute("city", mc.Groups["city"].Value.Trim());
                    objXmlElementCity.SetAttribute("weather", mc.Groups["weather"].Value.Trim());
                    objXmlElementCity.SetAttribute("temperature", mc.Groups["temperature"].Value.Trim());
                    objXmlElementCity.SetAttribute("windway", mc.Groups["windway"].Value.Trim());
                    objXmlElementCity.SetAttribute("wind", mc.Groups["wind"].Value.Trim());
                    objXmlCityList.AppendChild(objXmlElementCity);
                }
                catch (Exception ex)
                {
                    Yesun.Edzh.BLL.Log.LogHelper.WriteError(ex.Message);
                }
            }

            //南西
            content = GetContent("http://weather.news.sina.com.cn/images/figureWeather/map/southWest.html", "gb2312");
            for (mc = regex.Match(content), i = 1; mc.Success; mc = mc.NextMatch(), i++)
            {
                try
                {
                    XmlElement objXmlElementCity = objXml.CreateElement("city");
                    objXmlElementCity.SetAttribute("orders", "5");
                    objXmlElementCity.SetAttribute("city", mc.Groups["city"].Value.Trim());
                    objXmlElementCity.SetAttribute("weather", mc.Groups["weather"].Value.Trim());
                    objXmlElementCity.SetAttribute("temperature", mc.Groups["temperature"].Value.Trim());
                    objXmlElementCity.SetAttribute("windway", mc.Groups["windway"].Value.Trim());
                    objXmlElementCity.SetAttribute("wind", mc.Groups["wind"].Value.Trim());
                    objXmlCityList.AppendChild(objXmlElementCity);
                }
                catch (Exception ex)
                {
                    Yesun.Edzh.BLL.Log.LogHelper.WriteError(ex.Message);
                }
            }

            //北西
            content = GetContent("http://weather.news.sina.com.cn/images/figureWeather/map/northWest.html", "gb2312");
            for (mc = regex.Match(content), i = 1; mc.Success; mc = mc.NextMatch(), i++)
            {
                try
                {
                    XmlElement objXmlElementCity = objXml.CreateElement("city");
                    objXmlElementCity.SetAttribute("orders", "6");
                    objXmlElementCity.SetAttribute("city", mc.Groups["city"].Value.Trim());
                    objXmlElementCity.SetAttribute("weather", mc.Groups["weather"].Value.Trim());
                    objXmlElementCity.SetAttribute("temperature", mc.Groups["temperature"].Value.Trim());
                    objXmlElementCity.SetAttribute("windway", mc.Groups["windway"].Value.Trim());
                    objXmlElementCity.SetAttribute("wind", mc.Groups["wind"].Value.Trim());
                    objXmlCityList.AppendChild(objXmlElementCity);
                }
                catch (Exception ex)
                {
                    Yesun.Edzh.BLL.Log.LogHelper.WriteError(ex.Message);
                }
            }
            partten = "城市:(?<city>[^<]+) <br> 天氣:(?<weather>[^<]+)<br> 溫度:(?<temperature>[^<]+)<br>風力:(?<wind>[^<]+)<br>";
            regex = new Regex(partten, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            //北西 城市:武漢 <br> 天氣:小雨轉陰<br> 溫度:10 ℃~16 ℃<br>風力:小於3級<br>
            content = GetContent("http://weather.news.sina.com.cn/images/figureWeather/map/wholeNation.html", "gb2312");
            for (mc = regex.Match(content), i = 1; mc.Success; mc = mc.NextMatch(), i++)
            {
                try
                {
                    XmlElement objXmlElementCity = objXml.CreateElement("city");
                    objXmlElementCity.SetAttribute("orders", "7");
                    objXmlElementCity.SetAttribute("city", mc.Groups["city"].Value.Trim());
                    objXmlElementCity.SetAttribute("weather", mc.Groups["weather"].Value.Trim());
                    objXmlElementCity.SetAttribute("temperature", mc.Groups["temperature"].Value.Trim());
                    objXmlElementCity.SetAttribute("windway", "");
                    objXmlElementCity.SetAttribute("wind", mc.Groups["wind"].Value.Trim());
                    objXmlCityList.AppendChild(objXmlElementCity);
                }
                catch (Exception ex)
                {
                    Yesun.Edzh.BLL.Log.LogHelper.WriteError(ex.Message);
                }
            }

            objXml.DocumentElement.AppendChild(objXmlCityList);

            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.LogHelper.WriteError(ex.Message);
            }
            return str;
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.