[C#] 纯文本查看 复制代码 /// <summary>
/// 得到天气数据
/// </summary>
/// <returns>数组(0、天气;1、气温;2、风力;3、紫外线;4、空气)</returns>
public static string[] GetWeather()
{
Regex regex;
string[] weather = new string[5];
string content = "";
Match mcTmp;
Match mcCity;
int k = 1;
HttpWebResponse theResponse;
WebRequest theRequest;
//ss1.htm 注意:ss1-ss303代表了不同的城市,是不连续的
theRequest = WebRequest.Create("http://weather.news.qq.com/inc/ss1.htm");
try
{
theResponse = (HttpWebResponse)theRequest.GetResponse();
using (System.IO.Stream sm = theResponse.GetResponseStream())
{
System.IO.StreamReader read = new System.IO.StreamReader(sm, Encoding.Default);
content = read.ReadToEnd();
}
}
catch (Exception)
{
content = "";
}
string parttenTmp = "<td height=\"23\" width=\"117\" background=\"/images/r_tembg5.gif\" align=\"center\">(?<item1>[^<]+)</td>";
k = 1;
regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
{
weather[0] = mcTmp.Groups["item1"].Value;
}
parttenTmp = "height=\"23\" align=\"center\">(?<item1>[^/]+)</td>";
k = 1;
regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
{
weather[k] = mcTmp.Groups["item1"].Value;
}
return weather;
}
|