C#通过抓取页面获取当天天气信息
/// <summary>///得到天气数据
/// </summary>
/// <returns>数组(0、天气;1、气温;2、风力;3、紫外线;4、空气)</returns>
public static string[] GetWeather()
{
Regex regex;
string[] weather = new string;
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 = 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 = mcTmp.Groups["item1"].Value;
}
return weather;
}
小叶子 666 :victory:学到了
页:
[1]