现在的位置: 首页 > 综合 > 正文

C#用正则表达式获取网页中制定位置的内容

2013年01月20日 ⁄ 综合 ⁄ 共 925字 ⁄ 字号 评论关闭
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(); 

抱歉!评论已关闭.