日期:2014-05-17 浏览次数:20503 次
WebRequest myReq = WebRequest.Create("http://www.baidu.com/s?ie=gb2312&wd=" + GB2Unicode(“中国”) + "&pn=1&cl=3"); WebResponse myRes = myReq.GetResponse(); Stream resStream = myRes.GetResponseStream(); StreamReader sr = new StreamReader(resStream, Encoding.GetEncoding("utf-8")); result = sr.ReadToEnd(); myRes.Close(); resStream.Close(); sr.Close(); //读取html文件 MatchCollection allMatchResults = null; for (int i = 1; i <= n; i++) { string str = "id=\""+i.ToString()+"\""; if (result.IndexOf(str) >= 0) { result = result.Substring(result.IndexOf(str)); allMatchResults = Regex.Matches(result, "(<a[^>]*>).*?(</a>)", RegexOptions.IgnoreCase); if (allMatchResults.Count > 0) { string lianjie = allMatchResults[0].Value; lianjie = lianjie.Replace("background-image", ""); lianjie = lianjie.Replace("onmousedown=", ""); lianjie = lianjie.Replace("<em>", ""); lianjie = lianjie.Replace("</em>", ""); strs.Add(lianjie); } } } return strs;
------解决方案--------------------