天气预报API

聚合API

https://www.juhe.cn/docs

注册个人账号,实名认证后每天有100次免费请求额度。免费API中包括天气预报、新闻头条、笑话大全、万年历等。

天气预报API

https://www.juhe.cn/docs/api/id/73

接口地址:http://apis.juhe.cn/simpleWeather/query

返回格式:json

请求方式:http get/post

请求示例:http://apis.juhe.cn/simpleWeather/query?city=%E8%8B%8F%E5%B7%9E&key=

接口备注:通过城市名称或城市ID查询天气预报情况

请求参数说明:

| | 名称 | 必填 | 类型 | 说明 | | ---- | ---- | ---- | ------ | ------------------------------------------------------------ | | | city | 是 | string | 要查询的城市名称/id,城市名称如:温州、上海、北京,需要utf8 urlencode | | | key | 是 | string | 在个人中心->我的数据,接口名称上方查看 |

返回参数说明:

| | 名称 | 类型 | 说明 | | ---- | ----------- | ------ | ------------------------- | | | error_code | int | 返回码,0为查询成功 | | | reason | string | 返回说明 | | | result | string | 返回结果集 | | | - | - | - | | | realtime | - | 当前天气详情情况 | | | info | string | 天气情况,如:晴、多云 | | | wid | string | 天气标识id,可参考小接口2 | | | temperature | string | 温度,可能为空 | | | humidity | string | 湿度,可能为空 | | | direct | string | 风向,可能为空 | | | power | string | 风力,可能为空 | | | aqi | string | 空气质量指数,可能为空 | | | - | - | - | | | future | - | 近5天天气情况 | | | date | string | 日期 | | | temperature | string | 温度,最低温/最高温 | | | weather | string | 天气情况 | | | direct | string | 风向 |

JSON返回示例:

{"reason": "查询成功","result": {"city": "苏州","realtime": {"temperature": "4","humidity": "82","info": "阴","wid": "02","direct": "西北风","power": "3级","aqi": "80"},"future": [{"date": "2019-02-22","temperature": "1/7℃","weather": "小雨转多云","wid": {"day": "07","night": "01"},"direct": "北风转西北风"},{"date": "2019-02-23","temperature": "2/11℃","weather": "多云转阴","wid": {"day": "01","night": "02"},"direct": "北风转东北风"},{"date": "2019-02-24","temperature": "6/12℃","weather": "多云","wid": {"day": "01","night": "01"},"direct": "东北风转北风"},{"date": "2019-02-25","temperature": "5/12℃","weather": "小雨转多云","wid": {"day": "07","night": "01"},"direct": "东北风"},{"date": "2019-02-26","temperature": "5/11℃","weather": "多云转小雨","wid": {"day": "01","night": "07"},"direct": "东北风"}]},"error_code": 0
}

Linux下通过C获取天气

通过curl命令进行http get请求,并使用cJSON库解析返回结果

cJSON库:https://sourceforge.net/projects/cjson/

weather.c

#include <stdio.h>
#include <stdlib.h>
#include "include.h"//!!!使用时请换成自己的KEY
#define API_KEY "自己的KEY"
//输出菜单
int menu();
//请求对应城市的天气
int simpleWeather(int cityIndex);enum{BEJING = 0,SHANGHAI = 1,FUZHOU = 2,SHENZHENG = 3,CITY_COUNT,
};char *city[CITY_COUNT] = {"北京","上海","福州","深圳"
};int main(int argc, char const *argv[])
{int input = 0;while (1){menu();scanf("%d", &input);switch (input){case 1://printf("北京n");simpleWeather(BEJING);break;case 2://printf("上海n");simpleWeather(SHANGHAI);break;case 3://printf("福州n");simpleWeather(FUZHOU);break;default:printf("未知菜单,程序退出n");return 0;}}return 0;
}int menu()
{printf("天气预报n");printf("1.北京n");printf("2.上海n");printf("3.福州n");printf("输入序号获取天气:n");return 0;
}int simpleWeather(int cityIndex)
{if(cityIndex >= CITY_COUNT){printf("查询不到城市数据!n");return 0;}printf("查询城市 :%sn", city[cityIndex]);char cmd[MAX_LEN] = {0};char result[RESULT_BUF_LEN] = {0};//生成API请求urlsprintf(cmd, "curl -s "http://apis.juhe.cn/simpleWeather/query?city=%s&key=%s"", city[cityIndex], API_KEY);popenCmd(cmd, result);//printf("result :%sn", result);cJSON * root = NULL;cJSON * api_result = NULL, *api_realtime = NULL;root = cJSON_Parse(result);if(NULL == root){printf("解析json数据失败!n");return -1;}//先查找resultapi_result = cJSON_GetObjectItem(root, "result");//再查找realtimeapi_realtime = cJSON_GetObjectItem(api_result, "realtime");//printf("%sn", cJSON_Print(api_realtime));/*"realtime": {"temperature":  "30","humidity": "48","info": "阴","wid":  "02","direct":   "南风","power":    "2级","aqi":  "65"}*/cJSON *temp = cJSON_GetObjectItem(api_realtime, "temperature");cJSON *humidity = cJSON_GetObjectItem(api_realtime, "humidity");cJSON *info = cJSON_GetObjectItem(api_realtime, "info");cJSON *direct = cJSON_GetObjectItem(api_realtime, "direct");printf("温度:%s度n", temp->valuestring);printf("湿度:%s度n", humidity->valuestring);printf("天气:%sn", info->valuestring);printf("风向:%sn", direct->valuestring);return 0;
}

tools.c

目前读取popen结果使用最大缓存102400,最好修改代码将缓存改小,重复读取。

#include <stdio.h>
#include "include.h"int popenCmd(char *cmd, char *result)
{//运行命令,将结果保存到resultsFILE *fp = popen(cmd, "r");if (NULL == fp){printf("打开 file命令失败n");goto err;}//将结果读取到results字符串数组中fgets(result, RESULT_BUF_LEN, fp);if (fp){pclose(fp);fp = NULL;}return 0;err:if (fp){pclose(fp);fp = NULL;}return -1;
}

include.h

#include "cJSON.h"#define MAX_LEN 256
#define RESULT_BUF_LEN 102400//执行命令,将命令结果保存到result
int popenCmd(char *cmd, char *result);
#编译命令:
gcc weather.c cJSON.c tools.c -lm -o weather
./weather#输入结果:
天气预报
1.北京
2.上海
3.福州
输入序号获取天气:
1
查询城市 :北京
温度:25度
湿度:82度
天气:多云
风向:东北风

天气预报接口_001_天气预报API相关推荐

  1. 中国天气预报接口:SmartWeather API中key的计算方法

    最近申请到SmartWeatherAPI天气预报接口的使用权限,开始着手我的实时天气预报系统的开发,主要开发的版本使用的是Python脚本,成果将于近期以系列文章与大家见面.今天在这里我和大家探讨一下 ...

  2. php天气预报接口,php天气预报接口 可根据关键词获取天气

    php天气预报接口 可根据关键词获取天气2017-12-02 21:31 现在好多天气接口都要收费了,以前的免费的现在要么也加入收费行列,要么网站都已经无法打开不能用了. 今天小编分享的这个php天气 ...

  3. 小程序开发学习(4)---天气预报接口API篇

    文章目录 1.实现用户定位API 2.获取定位信息 1.实现用户定位API 天气预报API大多数都是需要收费的,但是在现在学习阶段可以不需要购买收费的,网上能找到免费的天气预报API地址,但是可能不是 ...

  4. html百度天气api,百度API 免费接口获取天气预报

    百度API 免费接口获取天气预报 发布时间:2020-08-06 05:38:12 来源:51CTO 阅读:4726 作者:大大果 Document #result{width: 800px;bord ...

  5. Vuejs模拟Ajax请求接口(天气预报API)跨域问题 - 案例篇

    vuejs的Ajax跨域请求问题一直都是前端人员开发vue项目进程中经常遇到的不得不解决的热门问题,也是个心病. 首先看一下,页面 报错内容提示: Access to XMLHttpRequest a ...

  6. 开源免费天气预报接口API(国家气象局提供)

    国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...

  7. 天气API-----开源免费天气预报接口API以及全国所有地区代码!!(国家气象局提供)

    国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...

  8. 中国天气网免费天气预报接口API

    国家气象局提供的天气预报接口 接口地址: 实时天气 http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn ...

  9. 开源免费天气预报接口API以及全国所有地区代码

    收藏一下 练手时用得上 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/ci ...

最新文章

  1. Oracle Database 11g的使用
  2. URAL 1993 This cheeseburger you don't need 模拟题
  3. 在浏览器中输入URL按下回车键后发生了什么
  4. 深度解析java内存原理
  5. Java 二维数组的初始化
  6. Linux系统的中断、系统调用和调度概述【转】
  7. Spring Boot 启动载入数据 CommandLineRunner
  8. ffmpeg-0.6.3 移植到 windows 开源代码
  9. 大话西游之仙侣履奇缘——[超经典台词]
  10. matlab中产生对角阵,关于matlab中的diag函数(矩阵对角元素的提取和创建对角阵)
  11. i510300h和i78750h参数对比哪个好
  12. 用文字描述给黑白照上色,这个免费网站火了!网友:比其他同类都好用
  13. 【设置版本号】React Native 通过配置文件package.json设置 Android安卓和iOS版本号
  14. 一寸照片电子版怎么弄?电子证件照制作方法
  15. C语言标准ANSI C、C语言的特点、C语言的关键字(32个)
  16. 【完美解决】org.apache.catalina.core.StandardContext.filterStart 启动过滤器异常
  17. java毕业设计家庭理财管理系统(附源码、数据库)
  18. Downloading NEX-GDDP data from google Earth Engine
  19. 黑客攻防专题三:名词介绍
  20. FLINK 基于1.15.2的Java开发-Watermark是怎么解决延迟数据唯一正确的生产级解决方案-目前市面上的例子都有问题

热门文章

  1. c++排序(CList类库)
  2. 高效人士的116个IT秘诀
  3. 摄像机模型和双目建模三维点云的理解
  4. 原版win7 install.wim在哪?
  5. Openwrt 18.06 iPhone XR usb tethering导致内核崩溃问题解决方案
  6. 安卓u盘格式化工具apk_华为智慧屏U盘安装APP分享
  7. Python每日一练-----难题唯唯诺诺,易题重拳出击?(回文数)
  8. Python 正则提取字符串中的地区
  9. ALV能否实现自动小计
  10. 微信公众号开发实战 | 01:环境配置