当前位置: 首页 > news >正文

基于网络技术的天气数据查询

背景描述:

天气数据不但影响着我们的各行各业,而且也和我们个人息息相关,能够快速的获取所需要的天气数据能够为我们的日常工作和生活提供非常大的便利。目前天气数据都会通过各种网站进行发布,虽然我们可以通过这些网站进行数据的浏览,但是通常这些数据都是零散的,不同网站的数据格式也是多种多样的,我们很难直接快速按照特定格式获取特定时间段的天气数据列表。

该系统将基于目前比较流行的网络爬虫技术,对网站上的天气数据进行查询分析,最终使客户能够通过简单的操作,快速,准确的获取目标天气数据。

功能描述:

该系统的功能主要包括两部分,第一部分是天气数据查询,包括时间段数据查询,实时天气数据查询;第二部分是打印查询出的天气数据

系统结构图:

模块介绍:

数据输入与显示(控制台): 主要用于用户输入天气数据查询的条件信息, 以及显示查询结果

数据检查模块: 用于检查用户输入的数据是否有效,格式是否正确

时间段数据查询: 用于收集时间段数据查询所需要的所有数据, 调用网络数据查询模块, 同时对网络数据查询模块的返回值进行整理, 使其按照规定的格式返回控制台进行显示, 并且把查询结果暂存到缓存区用于后续的打印。查询时间以用户输入的起始日期开始,输入的终止日期结束

实时数据查询: 用于收集实时数据查询所需要的所有数据, 调用网络数据查询模块, 同时对网络数据查询模块的返回值进行整理, 使其按照规定的格式返回控制台进行显示,并且把查询结果暂存到缓存区用于后续的打印,查询时间区间为以输入有效的终止时间的时刻为准的操作系统时间为起始时间, 以输入的终止时间为结束时间

数据打印模块: 当用户选择数据打印时, 从暂存区读出数据进行打印

功能实现:

#include<stdio.h>
#include <head.h>
#include "cJSON.h"int connect_server(void)
{int soc_fd = socket(AF_INET,SOCK_STREAM,0);if(soc_fd < 0){perror("socket error");return -1;}struct sockaddr_in seraddr;seraddr.sin_family = AF_INET;seraddr.sin_port = htons(80);seraddr.sin_addr.s_addr = inet_addr("103.205.5.228"); int fd = connect(soc_fd,(const struct sockaddr*)&seraddr,sizeof(seraddr));if(fd < 0){perror("connect fail");return -1;}return soc_fd;
}int cJsonA(void *buf_void)
{char *buf =index((char *)buf_void,'{');cJSON* cjson_test = NULL;cJSON* cjson_success = NULL;cJSON* cjson_result = NULL;cJSON* cjson_result_weaid = NULL;cJSON* cjson_result_days = NULL;cJSON* cjson_result_week = NULL;cJSON* cjson_result_citynm = NULL;cJSON* cjson_result_temperature = NULL;cJSON* cjson_result_temperature_curr = NULL;cJSON* cjson_result_humidity = NULL;cJSON* cjson_result_aqi = NULL;cJSON* cjson_result_weather = NULL;cJSON* cjson_result_weather_curr = NULL;cJSON* cjson_result_wind = NULL;cJSON* cjson_result_winp = NULL;cJSON* cjson_result_temp_high = NULL;cJSON* cjson_result_temp_low = NULL;cJSON* cjson_result_temp_curr = NULL;//printf("%s\n",buf);cjson_test = cJSON_Parse(buf);if(cjson_test == NULL){printf("parse fail.\n");return -1;    }cjson_success = cJSON_GetObjectItem(cjson_test,"success");cjson_result = cJSON_GetObjectItem(cjson_test,"result");cjson_result_weaid = cJSON_GetObjectItem(cjson_result,"weaid");cjson_result_days = cJSON_GetObjectItem(cjson_result,"days");cjson_result_week = cJSON_GetObjectItem(cjson_result,"week");cjson_result_citynm = cJSON_GetObjectItem(cjson_result,"citynm");cjson_result_temperature = cJSON_GetObjectItem(cjson_result,"temperature");cjson_result_temperature_curr = cJSON_GetObjectItem(cjson_result,"temperature_curr");cjson_result_humidity = cJSON_GetObjectItem(cjson_result,"humidity");cjson_result_aqi = cJSON_GetObjectItem(cjson_result,"aqi");cjson_result_weather = cJSON_GetObjectItem(cjson_result,"weather");cjson_result_weather_curr = cJSON_GetObjectItem(cjson_result,"weather_curr");cjson_result_wind = cJSON_GetObjectItem(cjson_result,"wind");cjson_result_winp = cJSON_GetObjectItem(cjson_result,"winp");cjson_result_temp_high = cJSON_GetObjectItem(cjson_result,"temp_high");cjson_result_temp_low = cJSON_GetObjectItem(cjson_result,"temp_low");cjson_result_temp_curr = cJSON_GetObjectItem(cjson_result,"temp_curr");printf("success:  %s\n",cjson_success->valuestring);printf("result:   %s\n",cjson_result->valuestring);printf("weaid:    %s\n",cjson_result_weaid->valuestring);printf("days:     %s\n",cjson_result_days->valuestring);printf("week:     %s\n",cjson_result_week->valuestring);printf("citynm:   %s\n",cjson_result_citynm->valuestring);printf("temperature:   %s\n",cjson_result_temperature->valuestring);printf("temperature_curr:    %s\n",cjson_result_temperature_curr->valuestring);printf("humidity:   %s\n",cjson_result_humidity->valuestring);printf("aqi:        %s\n",cjson_result_aqi->valuestring);printf("weather:    %s\n",cjson_result_weather->valuestring);printf("weather_curr:    %s\n",cjson_result_weather_curr->valuestring);printf("wind:       %s\n",cjson_result_wind->valuestring);printf("winp:       %s\n",cjson_result_winp->valuestring);printf("temp_high:  %s\n",cjson_result_temp_high->valuestring);printf("temp_low:   %s\n",cjson_result_temp_low->valuestring);cJSON_Delete(cjson_test);return 0;
}int cJsonB(void *buf_void)
{char *buf =index((char *)buf_void,'{');cJSON* cjson_test = NULL;cJSON* cjson_success = NULL;cjson_test = cJSON_Parse(buf);if(cjson_test == NULL){printf("parse fail.\n");return -1;}cJSON* cjson_result = cJSON_GetObjectItem(cjson_test,"result");if(!cjson_result || !cJSON_IsArray(cjson_result)){printf("result is not an array.\n");cJSON_Delete(cjson_test);return -1;}int result_array_size = cJSON_GetArraySize(cjson_result),i = 0;for(i = 0;i < result_array_size;++i){cJSON *cjson_result_item = cJSON_GetArrayItem(cjson_result,i);if(!cjson_result_item)continue;cJSON *cjson_item_days = cJSON_GetObjectItem(cjson_result_item,"days");cJSON *cjson_item_week = cJSON_GetObjectItem(cjson_result_item,"week");cJSON *cjson_item_temperature = cJSON_GetObjectItem(cjson_result_item,"temperature");cJSON *cjson_item_humidity = cJSON_GetObjectItem(cjson_result_item,"humidity");cJSON *cjson_item_temp_high = cJSON_GetObjectItem(cjson_result_item,"temp_high");cJSON *cjson_item_temp_low = cJSON_GetObjectItem(cjson_result_item,"temp_low");cJSON *cjson_item_humi_high = cJSON_GetObjectItem(cjson_result_item,"humi_high");cJSON *cjson_item_humi_low = cJSON_GetObjectItem(cjson_result_item,"humi_low");cJSON *cjson_item_weather = cJSON_GetObjectItem(cjson_result_item,"weather");printf("第%d天\n",i + 1);printf("days:     %s\n",cjson_item_days? cjson_item_days->valuestring : "N/A");printf("week:     %s\n",cjson_item_week ? cjson_item_week->valuestring : "N/A");printf("temperature:   %s\n",cjson_item_temperature ? cjson_item_temperature->valuestring : "N/A");printf("humidity:   %s\n",cjson_item_humidity ? cjson_item_humidity->valuestring : "N/A");printf("weather:    %s\n",cjson_item_weather ? cjson_item_weather->valuestring : "N/A");printf("temp_high:  %s\n",cjson_item_temp_high ? cjson_item_temp_high->valuestring : "N/A");printf("temp_low:   %s\n",cjson_item_temp_low ? cjson_item_temp_low->valuestring : "N/A");printf("humi_high:  %s\n",cjson_item_humi_high ? cjson_item_humi_high->valuestring : "N/A");printf("humi_low:   %s\n",cjson_item_humi_low ? cjson_item_humi_low->valuestring : "N/A");}cJSON_Delete(cjson_test);return 0;
}
char* linkPass(int i,int soc_fd,const char *weaId,char *buf)
{//char buf1[1024] = {0};memset(buf,0,1024 * 1024);snprintf(buf,1024,"GET /?app=%s&weaid=%s&appkey=73833&sign=32566b51caab63127b0bcb2db7b15be2&format=json HTTP/1.1\r\n",i == 1 ? "weather.today" : "weather.future",weaId);char *arg[] = {buf,"Host: api.k780.com\r\n","User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0\r\n","Accept-Language: en-US,en;q=0.5\r\n","Connection: close\r\n","Upgrade-Insecure-Requests: 1\r\n\r\n",NULL};int j = 0;while(arg[j] != NULL){write(soc_fd,arg[j],strlen(arg[j]));++j;}char tempbuf[1024] = {0};while(1){int ret = read(soc_fd,tempbuf,sizeof(tempbuf));if(ret <= 0){break;}strcat(buf,tempbuf);}return buf;
}int main(int argc,char *argv[])
{int fd = connect_server();if(fd < 0){return -1;}printf("   天气查询\n");printf("1:查询今日天气\n");printf("0:查询未来天气\n");int i = 0;scanf("%d",&i);printf("请输入城市\n");char s[1024] = {0};scanf("%s",s);char *buf = malloc(1024 * 1024);linkPass(i,fd,s,buf);if(buf != NULL){if(i == 1){cJsonA(buf);free(buf);}else if(i == 0){cJsonB(buf);free(buf);}}close(fd);return 0;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 在 Spring Boot 中为 MyBatis 添加拦截器
  • .Net 6.0--通用帮助类--FileHelper
  • python基础语法1
  • JavaSE ——类和对象
  • 并发式服务器
  • 最新国内Docker 安装
  • 数学建模学习(126):基于Python的最优最劣法(BWM)在多标准决策中的应用
  • 并发服务器---IO多路复用
  • 24暑假算法刷题 | Day39 | 动态规划 VII | LeetCode 198. 打家劫舍,213. 打家劫舍 II,337. 打家劫舍 III
  • mysql的group by怎么用
  • 后端面试真题整理
  • Flask 安装和应用
  • LVGL之FFmpeg使用
  • 扬尘监测算法全套源码扬尘监测算法识别训练样本展示
  • 视频提取字幕的软件有哪些?5款高识别率工具任你选
  • 分享一款快速APP功能测试工具
  • @jsonView过滤属性
  • Angular 2 DI - IoC DI - 1
  • css属性的继承、初识值、计算值、当前值、应用值
  • Django 博客开发教程 16 - 统计文章阅读量
  • Fundebug计费标准解释:事件数是如何定义的?
  • Github访问慢解决办法
  • JAVA之继承和多态
  • leetcode98. Validate Binary Search Tree
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • SpiderData 2019年2月23日 DApp数据排行榜
  • TypeScript实现数据结构(一)栈,队列,链表
  • Vue2.0 实现互斥
  • vue脚手架vue-cli
  • windows下mongoDB的环境配置
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • 半理解系列--Promise的进化史
  • 反思总结然后整装待发
  • 分类模型——Logistics Regression
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 基于游标的分页接口实现
  • 用jquery写贪吃蛇
  • 怎样选择前端框架
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • $NOIp2018$劝退记
  • (55)MOS管专题--->(10)MOS管的封装
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (回溯) LeetCode 46. 全排列
  • (十三)Flask之特殊装饰器详解
  • (图)IntelliTrace Tools 跟踪云端程序
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .gitignore文件使用
  • .NET 3.0 Framework已经被添加到WindowUpdate
  • .net core 管理用户机密
  • .NET Core 项目指定SDK版本