志在指尖
用双手敲打未来

C#通过抓取页面获取当天天气信息

///<summary>
///得到天气数据
///</summary>
///<returns>数组(0、天气;1、气温;2、风力;3、紫外线;4、空气)</returns>
publicstaticstring[]GetWeather()
{
Regexregex;
string[]weather=newstring[5];
stringcontent=””;
MatchmcTmp;
MatchmcCity;
intk=1;
HttpWebResponsetheResponse;
WebRequesttheRequest;
//ss1.htm注意:ss1-ss303代表了不同的城市,是不连续的
theRequest=WebRequest.Create(“http://weather.news.qq.com/inc/ss1.htm”);
try
{
theResponse=(HttpWebResponse)theRequest.GetResponse();
using(System.IO.Streamsm=theResponse.GetResponseStream())
{
System.IO.StreamReaderread=newSystem.IO.StreamReader(sm,Encoding.Default);
content=read.ReadToEnd();
}
}
catch(Exception)
{
content=””;
}
stringparttenTmp=”<tdheight=\”23\”width=\”117\”background=\”/images/r_tembg5.gif\”align=\”center\”>(?<item1>[^<]+)</td>”;
k=1;
regex=newRegex(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=newRegex(parttenTmp,RegexOptions.Compiled|RegexOptions.IgnoreCase);
for(mcTmp=regex.Match(content),k=1;mcTmp.Success;mcTmp=mcTmp.NextMatch(),k++)
{
weather[k]=mcTmp.Groups[“item1”].Value;
}
returnweather;
}

未经允许不得转载:IT技术网站 » C#通过抓取页面获取当天天气信息
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载