如何抓取网页中的实时监测数据进行分析

   使用wpf做了窗体,跟Silverlight开发环境一样,将前台设计与后台开发逻辑分离开来,抓取南京市九个PM 2.5监测站点的数据

   如何抓取网页中的实时监测数据进行分析_第1张图片

前台代码:

    
        
后台代码:

  

             class MonitorInfo
            {
               public  string Name{get;set;}
               public  string Density{get;set;}
            };

             List  myListString=new List();


            private string GetWebContent(string Url)
            {
                string strResult = "";
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                    //声明一个HttpWebRequest请求 
                    request.Timeout = 30000;
                    //设置连接超时时间 
                    request.Headers.Set("Pragma", "no-cache");
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream streamReceive = response.GetResponseStream();
                    Encoding encoding = Encoding.GetEncoding("GB2312");
                    StreamReader streamReader = new StreamReader(streamReceive, encoding);
                    strResult = streamReader.ReadToEnd();
                }
                catch
                {
                    System.Windows.Forms.MessageBox.Show("出错");
                }
                return strResult;
            } 

        private void button1_Click(object sender, RoutedEventArgs e)
        {
                textBox1.Text="http://www.pm2d5.com/city/nanjing.html";
                String Url = textBox1.Text;
                string strWebContent = GetWebContent(Url);
                int divIndex = strWebContent.IndexOf("weilai");
                int tableStartIndex = strWebContent.IndexOf("", tableStartIndex);
                string strWeb = strWebContent.Substring(tableStartIndex, tableEndIndex - tableStartIndex + 8);

                //生成HtmlDocument
                WebBrowser web = new WebBrowser();
                web.Navigate("about:blank");
                HtmlDocument htmldoc = web.Document.OpenNew(true);
                htmldoc.Write(strWeb);
                HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("tr");
                int i = 0;
                myListString.Clear();
                foreach (HtmlElement tr in htmlTR)
                {
                    if (i == 0)
                    {
                        i++;
                        continue;//跳过表头
                    }

                    MonitorInfo monitorInfo = new MonitorInfo();
                    monitorInfo.Name = tr.GetElementsByTagName("td")[0].InnerText.ToString();
                    monitorInfo.Density = tr.GetElementsByTagName("td")[3].InnerText.ToString();
                    myListString.Add(monitorInfo);
                }
                dataGrid1.ItemsSource = myListString;
            }
            
    }
         在MonitorInfo这个类的设计中,注意使用get,set,否则将List数据绑定到DataGrid后会出现空白行

你可能感兴趣的:(如何抓取网页中的实时监测数据进行分析)