I want to extract a label value from a website. I looked at the html source in Chrome and found the line:
<strong><span id="lbName">George</span></strong>
The label name lbName is unique in that request. But how do I extract the name “George” from this line? I looked at regular expressions, but so far it has only been if the string contains some pattern or not, which I already know it does.
public static void GetName()
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("Http://MyWebsite.com");
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
string sPattern = "lbName";
// extract the value of lbName ?
}
There is a library, Html Agility Pack. Use that. I’ll add that if you are always looking at the same page, and you know the page won’t change its format, you could simply use the
IndexOfmethod and search for<span id="lbName">. Something like: