c# - how to search through a WebRequest -
how search through response of webrequest set of characters. example making program gets closings school district , need webrequest word "... closed today" , output entire line of text. here got far. returns page in html (i believe):
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 request url. webrequest request = webrequest.create ( "http://www.nbcwashington.com/weather/school-closings/"); // if required server, set credentials. request.credentials = credentialcache.defaultcredentials; // response. webresponse response = request.getresponse (); // display status. console.writeline (((httpwebresponse)response).statusdescription); // stream containing content returned server. stream datastream = response.getresponsestream (); // open stream using streamreader easy access. streamreader reader = new streamreader (datastream); // read content. string responsefromserver = reader.readtoend (); text.text = responsefromserver; // clean streams , response. reader.close (); response.close(); } private void text_textchanged(object sender, eventargs e) { } } }
which returns bunch of stuff this:
<!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, , delays " /> <meta property="og:image" content="http://media.nbcwashington.com/images/school-closings-graphic.gif" />
also part im looking for:
<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, update page regularly you'll first know if school closing or have delays due bad weather. in meantime, check <a href='/weather/'>local weather</a> see if things change.</p> <p class="schoolopen"></p> </div> </div> </div>
your best bet use htmlagilitypack. google it.
example:
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, update page regularly you'll first know if school closing or have delays due bad weather. in meantime, check <a href='/weather/'>local weather</a> see if things 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);
Comments
Post a Comment