110,546
社区成员
发帖
与我相关
我的任务
分享
//通过URL取网页源代码
private string GetHtmlCode(string url, Encoding encoding)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, encoding);
string html = (sr.ReadToEnd());
resStream.Close();
sr.Close();
return html;
}
//调用,解析
string html = GetHtmlCode("http://www.baidu.com/s?wd=CNDN", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"百度一下,找到相关网页约(?<count>\d{1,3}(?:,\d{3})*)篇");
Match m = reg.Match(html);
if (m.Success)
{
richTextBox2.Text += m.Groups["count"].Value + "\n";
}
这个应该与数据库表进行联系,然后查询匹配的记录行数,时间就计算一下就好了,数据库中也可以查询
STRING SQL="SELECT COUNT(*) FROM TB WHERE COL LIKE '%"+TEXTBOX.TEXT+"%'";