如何抓取網頁中的實時監測資料進行分析
阿新 • • 發佈:2019-02-08
使用wpf做了窗體,跟Silverlight開發環境一樣,將前臺設計與後臺開發邏輯分離開來,抓取南京市九個PM 2.5監測站點的資料
前臺程式碼:
後臺程式碼:<Grid> <Button Content="資料獲取" Height="23" HorizontalAlignment="Left" Margin="487,23,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> <DataGrid AutoGenerateColumns="False" Height="338" HorizontalAlignment="Left" Margin="0,57,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="591" CanUserAddRows="False"> <DataGrid.Columns> <DataGridTextColumn Header="名稱" IsReadOnly="True" Binding="{Binding Name,Mode=TwoWay}" Width="200" /> <DataGridTextColumn Header="PM2.5濃度" IsReadOnly="True" Binding="{Binding Density,Mode=TwoWay}" Width="200"/> </DataGrid.Columns> </DataGrid> <Label Content="網址:" Height="28" HorizontalAlignment="Left" Margin="29,23,0,0" Name="label1" VerticalAlignment="Top" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="88,25,0,0" Name="textBox1" VerticalAlignment="Top" Width="362" /> </Grid>
在MonitorInfo這個類的設計中,注意使用get,set,否則將List資料繫結到DataGrid後會出現空白行class MonitorInfo { public string Name{get;set;} public string Density{get;set;} }; List<MonitorInfo> myListString=new List<MonitorInfo>(); 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("<table", divIndex); int tableEndIndex = strWebContent.IndexOf("</table>", 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; } }