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

本文介绍了一种通过网页信息过滤的方式获取特定城市天气预报的方法,包括最低和最高温度及全天不同时间段的天气状况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

最近做学生在线的首页,需要调用天气预报。一般情况下我们我们常常用提供商的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("&deg;""°");
                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;
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值