Read specific div from HttpResponse

2019-03-29 10:19发布

问题:

I am sending 1 httpWebRequest and reading the response. I am getting full page in the response. I want to get 1 div which is names ad Rate from the response. So how can I match that pattern?

My code is like:

    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://www.domain.com/");
    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
    Stream response = WebResp.GetResponseStream();
    StreamReader data = new StreamReader(response);
    string result = data.ReadToEnd();

I am getting response like:

<HTML><BODY><div id="rate">Todays rate 55 Rs.</div></BODY></HTML>

I want to read data of div rate. i.e. I should get Content "Todays rate 55 Rs."

So how can I make regex for this???

回答1:

The HTML Agility Pack can load and parse the file for you, no need for messy streams and responses:

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://jsbin.com/owobe3");
HtmlNode rateNode = doc.DocumentNode.SelectSingleNode("//div[@id='rate']");
string rate = rateNode.InnerText;


回答2:

You should read the entire response and then use something like the Html Agility Pack to parse the response and extract the bits you want in an xpath-like syntax:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(result);
var output = doc.DocumentNode.SelectSingleNode("//div[@id='rate']").InnerHtml;

Dont use regular expressions!



回答3:

If you have only one Todays rate text then you can do it like this:

Todays rate \d+ Rs.

In other case you can add div tag in your regex. Edit: Sorry, haven't installed regex locally You need to use grouping and get value from the group. It will look like this

<div id="rate">(?<group>[^<])</div>

Don't know if it works, however use this idea.