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

背景描述:

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

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

功能描述:

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

系统结构图:

模块介绍:

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

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

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

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

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

功能实现:

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值