如何搜索WebRequest

时间:2015-10-10 13:05:03

标签: c# html httpwebrequest httpwebresponse

如何通过webrequest对特定字符集的响应进行搜索。例如,我正在制作一个程序来解决我学区的关闭问题,我需要WebRequest查找单词" ...今天关闭"它将输出该文本的整行。这是我到目前为止所得到的。它以html(我相信)返回页面:

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>

1 个答案:

答案 0 :(得分:0)

您最好的选择是使用HTMLAgilityPack。谷歌吧。

示例:

    var=0
    while read i;
    do
      # perform computations on $i
      ((var++))
    done < <(find . -type f -name "*.bin" -maxdepth 1)