天气 api接口

50 篇文章 5 订阅

高德地图 天气api接口
https://lbs.amap.com/api/webservice/guide/tools/flowlevel

中国天气网-天气预报接口api
http://www.weatherdt.com/
http://www.weatherdt.com/market/datastore/database
http://www.weather.com.cn/data/sk/101110101.html
https://blog.csdn.net/a1943206465/article/details/54631615?utm_source=blogxgwz3
https://blog.csdn.net/itx2000/article/details/53835041
http://ah.weather.com.cn/zt/tqzt/2852238.shtml
https://github.com/BerlinSun/WeatherForecast
https://blog.csdn.net/lftaoyuan/article/details/78843472 国家气象局免费接口
https://blog.csdn.net/csdn_zsdf/article/details/74094696 利用气象局的接口写一个自己的天气预报

http://data.cma.cn/Market/MarketList.html
http://data.cma.cn/Market/Detail/code/A.0012.0001/type/1.html
http://data.cma.cn/Market/index.html 中国气象数据网
http://data.cma.cn/ 国家气象信息中心

http://wiki.swarma.net/index.php/彩云天气API/v2 彩云天气
https://openweathermap.org/

https://api.isoyu.com/#/
https://blog.csdn.net/hello_haozi/article/details/7564223
http://www.cma.gov.cn/2011qxfw/2011qtqyb/
https://www.cnblogs.com/wangjingblogs/p/3192953.html
https://www.seniverse.com/ 心知天气
https://blog.csdn.net/danfengw/article/details/77473784

http://www.heweather.com/documents/api/s6/weather-now 和风天气api接口
综合考虑,小编使用的是和风天气的api接口,免费,天气指标多,日免费次数1000次

使用中需要注意的是url的问题
第一种:根据城市获取城市地的天气
location=北京市的城市ID
https://free-api.heweather.com/s6/weather/now?&location=CN101010100&key=xxxxx
第二种:根据请求ip获取所在地的天气
https://free-api.heweather.com/s6/weather/now?&location=auto_ip&key=xxxxx

注:被官网上说的:parameters代表请求参数,包括必选和可选参数。所有请求参数均使用&进行分隔,参数值存在中文或特殊字符的情况,需要对参数进行 url encode。给坑了,之前一直将 海淀、北京市在线转urlencode,一直提示city unknown,坑。
https://blog.csdn.net/zengxingyuluo/article/details/60577029?utm_source=blogxgwz1
https://blog.csdn.net/Chen_xiaobao/article/details/78382893?locationNum=8&fps=1
https://github.com/wqxcloud/CoolEuropeWeather
https://blog.csdn.net/qq_41405257/article/details/80056873?utm_source=blogxgwz1
https://blog.csdn.net/Rlingge/article/details/61416769?utm_source=blogxgwz0
https://blog.csdn.net/w1355485804/article/details/79566577#commentsedit
https://www.cnblogs.com/dd-dd/p/5269751.html
https://blog.csdn.net/qq_41405257/article/details/80056873
https://blog.csdn.net/withawind/article/details/82459789
https://www.jianshu.com/p/e3e04cf3fc0f
https://blog.csdn.net/qq_39590763/article/details/83502192

  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用和天气API的C代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> #define MAX_CITY_NAME_LEN 100 #define MAX_WEATHER_LEN 2000 // 和天气API的基本信息 #define API_KEY "your_api_key" //替换为你的API Key #define API_URL "https://free-api.heweather.net/s6/weather/now" // 回调函数,用于处理HTTP响应 size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) { strcat((char *) userdata, ptr); // 将HTTP响应内容添加到userdata中 return size * nmemb; } int main() { // 从用户输入中获取城市名 char city[MAX_CITY_NAME_LEN]; printf("请输入城市名:"); scanf("%s", city); // 构造HTTP请求URL char url[MAX_WEATHER_LEN]; snprintf(url, MAX_WEATHER_LEN, "%s?key=%s&location=%s", API_URL, API_KEY, city); // 初始化CURL库和HTTP请求 CURL *curl = curl_easy_init(); if (!curl) { fprintf(stderr, "初始化CURL失败!\n"); return EXIT_FAILURE; } curl_easy_setopt(curl, CURLOPT_URL, url); // 设置HTTP请求URL curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); // 设置回调函数 char weather[MAX_WEATHER_LEN] = ""; // 用于存储HTTP响应内容的字符串 curl_easy_setopt(curl, CURLOPT_WRITEDATA, weather); // 设置回调函数的userdata // 执行HTTP请求 CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "HTTP请求失败:%s\n", curl_easy_strerror(res)); return EXIT_FAILURE; } // 解析HTTP响应中的天气信息 char *status_start = strstr(weather, "\"status\":\""); // 找到状态码的起始位置 if (!status_start) { fprintf(stderr, "无法解析HTTP响应:%s\n", weather); return EXIT_FAILURE; } char *status_end = strchr(status_start + 11, '\"'); // 找到状态码的终止位置 if (!status_end) { fprintf(stderr, "无法解析HTTP响应:%s\n", weather); return EXIT_FAILURE; } *status_end = '\0'; // 将状态码终止位置的字符改为'\0',截断字符串 int status_code = atoi(status_start + 11); // 将状态码从字符串转换为整数 if (status_code != 200) { fprintf(stderr, "HTTP请求失败,状态码:%d\n", status_code); return EXIT_FAILURE; } char *cond_start = strstr(weather, "\"cond_txt\":\""); // 找到天气状况的起始位置 if (!cond_start) { fprintf(stderr, "无法解析HTTP响应:%s\n", weather); return EXIT_FAILURE; } char *cond_end = strchr(cond_start + 13, '\"'); // 找到天气状况的终止位置 if (!cond_end) { fprintf(stderr, "无法解析HTTP响应:%s\n", weather); return EXIT_FAILURE; } *cond_end = '\0'; // 将天气状况终止位置的字符改为'\0',截断字符串 char *cond_txt = cond_start + 13; // 天气状况的字符串就是起始位置的后面13个字符 printf("%s的天气状况为:%s\n", city, cond_txt); // 清理CURL库 curl_easy_cleanup(curl); return EXIT_SUCCESS; } ``` 需要注意的是,上述代码使用了libcurl库来进行HTTP请求,并使用了C标准库中的字符串处理函数来解析HTTP响应内容。在使用前需要安装libcurl库,并在编译时链接该库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值