C#用Regex擷取網頁中制定位置的內容
來源:互聯網
上載者:User
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();