C#实现简单的天气预报示例代码
作者:狗狗狗狗狗乐啊 时间:2022-03-22 22:52:59
前言
本来是打算用C#爬取天气网站上的信息,然后用正则表达过滤有用信息的,但是很淦,正则表达式太难了。无意间找到添加web引用的方式来获取天气信息,亲自测试后发现效果尚可,就记录一下。
引用部分
由于本次是控制台应用,就没有页面设计了。在VS中新建控制台程序后,右击“引用”——“添加服务引用”。
在“添加服务引用”左下角选择“高级”。
在“服务引用设置中”选择左下角的“添加web引用”。
在其中输入天气预报提取网址的url:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
至此,引用功能就已完成。该网站提供了很多查询方法,此处我们使用的是getWeatherCityName(),方法详细内容如下:
当然,你也可以转到该url查看更多定义,选择适合你的方法。
代码实现部分
Main方法里直接引用:
WeatherWebService myweather = new WeatherWebService();
string[] myweathers = myweather.getWeatherbyCityName("郑州");
for (int i = 0; i < myweathers.Length;i++ )
{ Console.WriteLine(myweathers[i]); }
传入的值尽量不要带“市”,返回的数组循环输出后结果如下:
其中各项所代表的含义可以查看上方官网的说明,这里为了让布局好看一点,让重点突出一点,可以使用到控制台的字体颜色转换语句:
Console.ForegroundColor = ConsoleColor.颜色;
完善后的代码如下:
WeatherWebService myweather = new WeatherWebService();
string[] myweathers = myweather.getWeatherbyCityName("郑州");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("今日天气:\n更新时间:" + myweathers[4]);
Console.WriteLine("当前选择地区:" + myweathers[0] + "_" + myweathers[1] + "\n");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(myweathers[6] + "(今日) 风向&风力:" + myweathers[7]);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" 气温:" + myweathers[5] + "\n");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("当前实况(数据每2.5小时左右自动更新一次):\n" + myweathers[10] + myweathers[11]);
Console.Write(myweathers[13] + "(明天) 风向&风力:" + myweathers[14]);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" 气温:" + myweathers[12] + "\n");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(myweathers[18] + "(后天) 风向&风力:" + myweathers[19]);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" 气温:" + myweathers[17] + "\n");
Console.ForegroundColor = ConsoleColor.White;
当然,你也可以在后面加一个判断,输入1的话可以查询其他城市,然后获取输入的值,传入方法中。如果感兴趣的话可以看一下源码,这里就不再过多展示。
运行效果
结语
程序很小,仅作分享。不足之处,望见谅。
项目源码:请别抢我闪刀姬/天气预报
来源:https://blog.csdn.net/weixin_44122062/article/details/105251106
标签:C#,天气预报
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java原生服务器接收上传文件 不使用MultipartFile类
2023-11-09 19:31:36
C# 使用Microsoft Edge WebView2的相关总结
2023-02-09 16:51:12
SSH框架网上商城项目第2战之基本增删查改、Service和Action的抽取
2023-06-21 19:16:23
![](https://img.aspxhome.com/file/2023/4/57494_0s.png)
springmvc 中dao层和service层的区别说明
2022-07-26 00:55:41
Java递归寻路实现,你真的理解了吗
2022-09-17 02:24:34
![](https://img.aspxhome.com/file/2023/5/69105_0s.png)
Spring如何利用@Value注解读取yml中的map配置
2023-07-24 21:18:00
Java非侵入式API接口文档工具apigcc用法详解
2023-11-24 10:01:00
![](https://img.aspxhome.com/file/2023/1/59331_0s.png)
SpringBoot定时任务两种(Spring Schedule 与 Quartz 整合 )实现方法
2023-11-01 16:03:39
Spring Cloud Ribbon客户端详细介绍
2023-11-27 21:36:22
![](https://img.aspxhome.com/file/2023/7/89947_0s.png)
Java注解详细介绍
2021-10-09 19:52:28
详解Spring ApplicationContext加载过程
2023-07-17 01:07:43
SpringBoot如何整合redis实现过期key监听事件
2023-08-04 18:51:19
![](https://img.aspxhome.com/file/2023/8/57808_0s.png)
javac -encoding 用法详解
2022-06-28 08:58:08
一文详解Java抽象类到底有多抽象
2023-08-27 01:41:26
![](https://img.aspxhome.com/file/2023/8/62168_0s.png)
WPF+SkiaSharp实现自绘拖曳小球
2023-03-25 01:31:14
![](https://img.aspxhome.com/file/2023/6/91286_0s.gif)
基于JAVA中Jersey处理Http协议中的Multipart的详解
2021-06-15 13:43:57
在WPF中实现全局快捷键功能
2023-12-02 07:45:39
c#多线程之线程基础
2022-10-06 20:34:44
Java五种方式实现多线程循环打印问题
2023-03-07 20:34:12
Java Spring处理循环依赖详解
2022-07-14 05:06:16