c# - downloading web pages html into html document -
i want html of web page. html there 2 elements who's xpath have want read. have little 0 knowledge on topic.
when searching keep seeing examples load url , put html string. believe since have 2 xpath's better download html of web page html document rather string or wrong?
using (webclient client = new webclient()) { string s = client.downloadstring(url); }
so how download html of webpage html document can search?
this how this.
- so first define url in string variable.
- then download string
httpwebrequest
class. - i use
htmlagilitypack
, should include in project (using nugger example). - create object of
htmldocument
, , load data object. now can navigate on
htmldocument
.string urladdress = "url.com"; httpwebrequest request = (httpwebrequest)webrequest.create(urladdress); httpwebresponse response = (httpwebresponse)request.getresponse(); string data = ""; if (response.statuscode == httpstatuscode.ok) { stream receivestream = response.getresponsestream(); streamreader readstream = null; if (response.characterset == null) { readstream = new streamreader(receivestream); } else { readstream = new streamreader(receivestream, encoding.getencoding(response.characterset)); } data = readstream.readtoend(); response.close(); readstream.close(); } htmldocument document2 = new htmlagilitypack.htmldocument(); document2.loadhtml(data);
Comments
Post a Comment