筛选网页信息获取天气预报
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;
}
}
}
但学生在线是用的教育网调用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;
}
}
}
相关文章推荐
- 在网页中获取天气预报信息
- 正则表达式相关:C# 抓取网页类(获取网页中所有信息)
- 微信公众平台(3)-网页授权获取用户基本信息
- 基于Springboot的微信公众号接入、通过网页授权机制获取用户信息
- 网页中获取微信粉丝信息
- C#获取网页上的控件信息
- C#微信公众号开发-高级接口-之网页授权oauth2.0获取用户基本信息(二)
- Python的学习(十九)--获取网页信息(一)
- 微信网页第三方登陆 获取用户信息 代码
- 微信开发笔记——微信网页登录授权,获取用户信息
- 网页信息抓取-如何获取延迟加载的网页数据
- 用python怎样爬网页呢?其实就是根据URL来获取它的网页信息!
- 微信--网页授权获取用户基本信息
- EmailSpider-----获取网页上邮件的信息
- 微信公众号开发——网页授权 (页面分享 、获取用户基本信息)
- 微信网页授权获取用户基本信息
- inputstream获取网页信息
- 第八篇 :微信公众平台开发实战Java版之如何网页授权获取用户基本信息
- 微信网页中获取用户基本信息(网页授权操作)
- c# 获取天气预报信息