网页抓取的一个小例子
protected void GetHtml() { string url = "http://www.baidu.com/"; HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url); //设置请求的url wRequest.Method = "Post"; wRequest.ContentType = "application/x-www-form-urlencoded"; wRequest.Timeout = 1000*60; byte[] charbyte = System.Text.Encoding.Default.GetBytes(url); //格式化参数 wRequest.ContentLength = charbyte.Length; StreamWriter sw = new StreamWriter(wRequest.GetRequestStream()); //字节流 try { sw.Write(url); }catch(Exception ex ){ }finally { sw.Close(); } StreamReader sr = new StreamReader(wRequest.GetResponse().GetResponseStream()); //开始接受服务器回传 string responseData = sr.ReadToEnd(); backResponse.InnerText = responseData; //得到结果 }
来给结果上个图,有图有真相:
也许你说,这么一大堆乱码,看着就头疼,怎么处理呢?下篇我们讲学习正则表达式,谢谢!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。