2015-10-10 80 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);