您的位置:首页 > 其它

筛选网页信息获取天气预报

2007-09-07 20:42 387 查看
最近做学生在线的首页,需要调用天气预报。一般情况下我们我们常常用提供商的WebSerivce。
但学生在线是用的教育网调用WebSerivce常常出现问题,我参考翔工作室技术部老源码,于是做了个网页信息的过滤成功的获取了天气信息!

/// <summary>
/// 天气预报
/// </summary>
private static void GetTian7OnlineWeather()
{
try
{

WebRequest wreq = WebRequest.Create("http://www.t7online.com/karten/html/JSU00.htm");

HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();

string HTML = "";
Stream s = wresp.GetResponseStream();

StreamReader objReader = new StreamReader(s, System.Text.Encoding.Default);
HTML = objReader.ReadToEnd();
if (HTML == null || HTML == "")
return;

string AllWeather = "", lowest1, lowest2, highest1, highest2, morning1, morning2, afternoon1, afternoon2, night1, night2;
int start, stop;
//HTML=HTML.ToLower();
//取得城市天气的地址
start = HTML.IndexOf("徐州", 0);
start = HTML.IndexOf("href="", start);
stop = HTML.IndexOf(""", start + 6);
string href = HTML.Substring(start + 6, stop - start - 6);
//获取页面数据
href = "http://www.t7online.com" + href;
wreq = WebRequest.Create(href);
wresp = (HttpWebResponse)wreq.GetResponse();

HTML = "";
s = wresp.GetResponseStream();

objReader = new StreamReader(s, System.Text.Encoding.Default);
HTML = objReader.ReadToEnd();
if (HTML == null || HTML == "")
return;
HTML = HTML.ToLower();

start = HTML.IndexOf("最低<br>温度", 0);
start = HTML.IndexOf("<b>", start);
stop = HTML.IndexOf("</b>", start);
lowest1 = HTML.Substring(start + 3, stop - start - 3);

start = HTML.IndexOf("<b>", stop);
stop = HTML.IndexOf("</b>", start);
lowest2 = HTML.Substring(start + 3, stop - start - 3);

start = HTML.IndexOf("最高<br>温度", 0);
start = HTML.IndexOf("<b>", start);
stop = HTML.IndexOf("</b>", start);
highest1 = HTML.Substring(start + 3, stop - start - 3);

start = HTML.IndexOf("<b>", stop);
stop = HTML.IndexOf("</b>", start);
highest2 = HTML.Substring(start + 3, stop - start - 3);

start = HTML.IndexOf("上午", 0);
start = HTML.IndexOf("title="", start);
stop = HTML.IndexOf(""", start + 7);
morning1 = HTML.Substring(start + 7, stop - start - 7);

start = HTML.IndexOf("title="", start);
stop = HTML.IndexOf(""", start + 7);
morning2 = HTML.Substring(start + 7, stop - start - 7);

start = HTML.IndexOf("下午", 0);
start = HTML.IndexOf("title="", start);
stop = HTML.IndexOf(""", start + 7);
afternoon1 = HTML.Substring(start + 7, stop - start - 7);

start = HTML.IndexOf("title="", start);
stop = HTML.IndexOf(""", start + 7);
afternoon2 = HTML.Substring(start + 7, stop - start - 7);

start = HTML.IndexOf("晚上", 0);
start = HTML.IndexOf("title="", start);
stop = HTML.IndexOf(""", start + 7);
night1 = HTML.Substring(start + 7, stop - start - 7);

start = HTML.IndexOf("title="", start);
stop = HTML.IndexOf(""", start + 7);
night2 = HTML.Substring(start + 7, stop - start - 7);

lowest1.Replace("<font color="#0060ff">", "");
lowest1.Replace("</font>", "");
highest1 = highest1.Replace("<font face="夹发砰" size="2"><font color="#ff0000">", "");
highest1 = highest1.Replace("</font>", "");
lowest2.Replace("<font color="#0060ff">", "");
lowest2.Replace("</font>", "");
highest2 = highest1.Replace("<font face="夹发砰" size="2"><font color="#ff0000">", "");
highest2 = highest1.Replace("</font>", "");

AllWeather = "<Table> <Title>今日天气</Title> <Lowest>" + lowest1 + "</Lowest> <Highest>" + highest1 + "</Highest> <Morning>" + morning1 + "</Morning> <Afternoon>" + afternoon1 + "</Afternoon> <Night>" + night1 + "</Night> </Table> ";
AllWeather = AllWeather + "<Table> <Title>明日天气</Title> <Lowest>" + lowest2 + "</Lowest> <Highest>" + highest2 + "</Highest> <Morning>" + morning2 + "</Morning> <Afternoon>" + afternoon2 + "</Afternoon> <Night>" + night2 + "</Night> </Table>";
AllWeather = AllWeather.Replace("°", "°");
AllWeather = "<?xml version="1.0" encoding="gb2312" ?> <weather> " + AllWeather + " </weather>";
string path = @"OnlineWeather.xml";
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs, Encoding.Default);
writer.WriteLine(AllWeather);
writer.Close();
fs.Close();
}
catch
{
System.Console.WriteLine("网络连接失败!");
System.Console.ReadLine();
return;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: