C#用正则表达式获取网页中制定位置的内容
string rl;
WebRequest myReq = WebRequest.Create("http://weather.news.qq.com/inc/ss125.htm");
WebResponse myRes = myReq.GetResponse();
Stream resStream = myRes.GetResponseStream();
StreamReader sr = new StreamReader(resStream, Encoding.Default);
StringBuilder sb = new StringBuilder();
while ((rl = sr.ReadLine()) != null)
{
sb.Append(rl);
}
string abc = "<td height=\"23\" align=\"center\"><img src=\"/images/tem1.gif\" width=\"57\" height=\"13\"></td><td height=\"23\" align=\"center\">";
string bcd = "<td height=\"23\" width=\"117\" background=\"/images/r_tembg5.gif\" align=\"center\">";
string regexStr = @"" + abc + "([^<]*)</td>";
Match mc = Regex.Match(sb.ToString(), regexStr, RegexOptions.IgnoreCase);
string regexStr1 = @"" + bcd + "([^<]*)</td>";
Match mc1 = Regex.Match(sb.ToString(), regexStr1, RegexOptions.IgnoreCase);
this.Label1.Text = "气温 " + mc.Groups[1].Value + " " + mc1.Groups[1].Value;
myRes.Close();
WebRequest myReq = WebRequest.Create("http://weather.news.qq.com/inc/ss125.htm");
WebResponse myRes = myReq.GetResponse();
Stream resStream = myRes.GetResponseStream();
StreamReader sr = new StreamReader(resStream, Encoding.Default);
StringBuilder sb = new StringBuilder();
while ((rl = sr.ReadLine()) != null)
{
sb.Append(rl);
}
string abc = "<td height=\"23\" align=\"center\"><img src=\"/images/tem1.gif\" width=\"57\" height=\"13\"></td><td height=\"23\" align=\"center\">";
string bcd = "<td height=\"23\" width=\"117\" background=\"/images/r_tembg5.gif\" align=\"center\">";
string regexStr = @"" + abc + "([^<]*)</td>";
Match mc = Regex.Match(sb.ToString(), regexStr, RegexOptions.IgnoreCase);
string regexStr1 = @"" + bcd + "([^<]*)</td>";
Match mc1 = Regex.Match(sb.ToString(), regexStr1, RegexOptions.IgnoreCase);
this.Label1.Text = "气温 " + mc.Groups[1].Value + " " + mc1.Groups[1].Value;
myRes.Close();