2015-10-10 117 views
-1

如何通過網絡請求的響應搜索特定字符集。例如,我正在製作一個程序,讓我的學區關閉,我需要WebRequest來查找「...今天已關閉」這個詞,它會輸出整個文本行。這是我到目前爲止。它返回的HTML頁面(我相信):如何通過WebRequest進行搜索

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using System.Net; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 

    } 

    private void label1_Click(object sender, EventArgs e) 
    { 

    } 

    private void button_Click(object sender, EventArgs e) 
    { 
     get_closings(); 
    } 

    private void get_closings() 
    { 

     // Create a request for the URL. 
     WebRequest request = WebRequest.Create (
      "http://www.nbcwashington.com/weather/school-closings/"); 
     // If required by the server, set the credentials. 
     request.Credentials = CredentialCache.DefaultCredentials; 
     // Get the response. 
     WebResponse response = request.GetResponse(); 
     // Display the status. 
     Console.WriteLine (((HttpWebResponse)response).StatusDescription); 
     // Get the stream containing content returned by the server. 
     Stream dataStream = response.GetResponseStream(); 
     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader (dataStream); 
     // Read the content. 
     string responseFromServer = reader.ReadToEnd(); 
     text.Text = responseFromServer; 
     // Clean up the streams and the response. 
     reader.Close(); 
     response.Close(); 

    } 

    private void text_TextChanged(object sender, EventArgs e) 
    { 

    } 
} 
} 

與這樣的一堆東西返回:

<!DOCTYPE html> 
<html lang=""> 
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#"> 

<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
<meta http-equiv="imagetoolbar" content="false"> 
<meta name="MSSmartTagsPreventParsing" content="true"> 
<!--p_theUserAgent - ${p_theUserAgent}--> 
<meta name="robots" content="noodp,noydir" /> 
<meta property="fb:app_id" content="168641655540" /> 
<meta property="og:url"    content="http://www.nbcwashington.com/weather/school-closings/"/> 
<meta property="og:site_name" content="NBC4 Washington" /> 
<meta property="og:title" content="School Closings, Cancellations, and Delays "  /> 
<meta property="og:image" content="http://media.nbcwashington.com/images/school-closings-graphic.gif" /> 

而且部分即時尋找:

<h1>School Closings</h1> 

      <div id="schoolForecast"> 


              <h3 style="font-family:'Arimo',Arial,sans-serif; color:#000; font-size:14px;">Forecast: School's Open.</h3> 
     <p class="schoolOpen">But don't worry, we update this page regularly so you'll be the first to know if your school is closing or will have delays due to bad weather. In the meantime, check your <a href='/weather/'>local weather</a> to see if things will change.</p> 
       <p class="schoolOpen"></p> 
          </div> 

     </div> 
</div> 

回答

0

最好的辦法是使用HTMLAgilityPack。谷歌爲它。

實施例:

String HTML = @" <h1>School Closings</h1> <div id=""schoolForecast""> <h3 style=""font-family:'Arimo',Arial,sans-serif; color:#000; font-size:14px;"">Forecast: School's Open.</h3> 
        <p class=""schoolOpen"">But don't worry, we update this page regularly so you'll be the first to know if your school is closing or will have delays due to bad weather. In the meantime, check your <a href='/weather/'>local weather</a> to see if things will change.</p> 
    <p class=""schoolOpen""></p> </div> </div> </div>"; 

HtmlAgilityPack.HtmlDocument Doc = new HtmlAgilityPack.HtmlDocument(); 
Doc.LoadHtml(HTML); 

HtmlNode Node = Doc.GetElementbyId("schoolForecast"); 

MessageBox.Show(Node.InnerText);