一、接口介绍:
1、API地址:
付费用户:https://api.heweather.com/v5/
免费用户:https://free-api.heweather.com/v5/
2、请求示例:
https://free-api.heweather.com/v5/weather?city=CN101010100&key=你的key
和风天气支持多语言,可以使用lang参数在调用数据时设定需要的语言。比如:
https://free-api.heweather.com/v5/weather?city=CN101010100&key=你的key&lang=en
默认国内城市采用简体中文,海外城市采用英文,默认状态不用添加lang参数。
3、数据返回实例:
{ "HeWeather5": [ { "aqi": { "city": { "aqi": "150", "co": "2", "no2": "76", "o3": "50", "pm10": "143", "pm25": "115", "qlty": "轻度污染", "so2": "50" } }, "basic": { "city": "北京", "cnty": "中国", "id": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": { "loc": "2016-03-12 17:50", "utc": "2016-03-12 09:50" } }, "daily_forecast": [ { "astro": { "sr": "06:30", "ss": "18:18" }, "cond": { "code_d": "101", "code_n": "100", "txt_d": "多云", "txt_n": "晴" }, "date": "2016-03-09", "hum": "9", "pcpn": "0.0", "pop": "0", "pres": "1034", "tmp": { "max": "9", "min": "2" }, "vis": "10", "wind": { "deg": "320", "dir": "无持续风向", "sc": "微风", "spd": "4" } }, { "astro": { "sr": "06:28", "ss": "18:19" }, "cond": { "code_d": "100", "code_n": "100", "txt_d": "晴", "txt_n": "晴" }, "date": "2016-03-10", "hum": "14", "pcpn": "0.0", "pop": "0", "pres": "1030", "tmp": { "max": "12", "min": "-2" }, "vis": "10", "wind": { "deg": "309", "dir": "北风", "sc": "3-4", "spd": "10" } }, { "astro": { "sr": "06:26", "ss": "18:20" }, "cond": { "code_d": "100", "code_n": "101", "txt_d": "晴", "txt_n": "多云" }, "date": "2016-03-11", "hum": "12", "pcpn": "0.0", "pop": "0", "pres": "1023", "tmp": { "max": "15", "min": "4" }, "vis": "10", "wind": { "deg": "201", "dir": "无持续风向", "sc": "微风", "spd": "3" } }, { "astro": { "sr": "06:25", "ss": "18:21" }, "cond": { "code_d": "101", "code_n": "101", "txt_d": "多云", "txt_n": "多云" }, "date": "2016-03-12", "hum": "20", "pcpn": "0.0", "pop": "0", "pres": "1015", "tmp": { "max": "15", "min": "4" }, "vis": "10", "wind": { "deg": "101", "dir": "无持续风向", "sc": "微风", "spd": "6" } }, { "astro": { "sr": "06:23", "ss": "18:22" }, "cond": { "code_d": "101", "code_n": "101", "txt_d": "多云", "txt_n": "多云" }, "date": "2016-03-13", "hum": "12", "pcpn": "0.0", "pop": "0", "pres": "1023", "tmp": { "max": "17", "min": "6" }, "vis": "10", "wind": { "deg": "338", "dir": "无持续风向", "sc": "微风", "spd": "10" } }, { "astro": { "sr": "06:22", "ss": "18:23" }, "cond": { "code_d": "101", "code_n": "104", "txt_d": "多云", "txt_n": "阴" }, "date": "2016-03-14", "hum": "11", "pcpn": "0.0", "pop": "0", "pres": "1018", "tmp": { "max": "18", "min": "6" }, "vis": "10", "wind": { "deg": "197", "dir": "无持续风向", "sc": "微风", "spd": "3" } }, { "astro": { "sr": "06:20", "ss": "18:24" }, "cond": { "code_d": "104", "code_n": "100", "txt_d": "阴", "txt_n": "晴" }, "date": "2016-03-15", "hum": "12", "pcpn": "0.0", "pop": "0", "pres": "1016", "tmp": { "max": "18", "min": "6" }, "vis": "10", "wind": { "deg": "125", "dir": "无持续风向", "sc": "微风", "spd": "2" } } ], "hourly_forecast": [ { "date": "2016-03-12 01:00", "hum": "24", "pop": "0", "pres": "1021", "tmp": "1", "wind": { "deg": "170", "dir": "南风", "sc": "微风", "spd": "8" } }, { "date": "2016-03-12 04:00", "hum": "32", "pop": "0", "pres": "1020", "tmp": "1", "wind": { "deg": "119", "dir": "东南风", "sc": "微风", "spd": "6" } }, { "date": "2016-03-12 07:00", "hum": "37", "pop": "0", "pres": "1019", "tmp": "1", "wind": { "deg": "67", "dir": "东北风", "sc": "微风", "spd": "7" } }, { "date": "2016-03-12 10:00", "hum": "30", "pop": "0", "pres": "1018", "tmp": "4", "wind": { "deg": "66", "dir": "东北风", "sc": "微风", "spd": "8" } }, { "date": "2016-03-12 13:00", "hum": "22", "pop": "0", "pres": "1016", "tmp": "9", "wind": { "deg": "91", "dir": "东风", "sc": "微风", "spd": "6" } }, { "date": "2016-03-12 16:00", "hum": "19", "pop": "0", "pres": "1015", "tmp": "11", "wind": { "deg": "118", "dir": "东南风", "sc": "微风", "spd": "6" } }, { "date": "2016-03-12 19:00", "hum": "24", "pop": "0", "pres": "1016", "tmp": "9", "wind": { "deg": "214", "dir": "西南风", "sc": "微风", "spd": "4" } }, { "date": "2016-03-12 22:00", "hum": "22", "pop": "0", "pres": "1018", "tmp": "6", "wind": { "deg": "303", "dir": "西北风", "sc": "微风", "spd": "12" } } ], "now": { "cond": { "code": "101", "txt": "多云" }, "fl": "-8", "hum": "32", "pcpn": "0", "pres": "1035", "tmp": "9", "vis": "10", "wind": { "deg": "340", "dir": "西南风", "sc": "3-4", "spd": "10" } }, "status": "ok", "suggestion": { "comf": { "brf": "较舒适", "txt": "白天天气晴好,早晚会感觉偏凉,午后舒适、宜人。" }, "cw": { "brf": "较适宜", "txt": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。" }, "drsg": { "brf": "较冷", "txt": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。" }, "flu": { "brf": "较易发", "txt": "天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。" }, "sport": { "brf": "较不宜", "txt": "天气较好,但考虑天气寒冷,推荐您进行室内运动,户外运动时请注意保暖并做好准备活动。" }, "trav": { "brf": "适宜", "txt": "天气较好,同时又有微风伴您一路同行。虽会让人感觉有点凉,但仍适宜旅游,可不要错过机会呦!" }, "uv": { "brf": "最弱", "txt": "属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。" } } } ] }
4、json数据格式整理
o-JSON
a-HeWeather data service 3.0
o-[0]
o-aqi
o-basic
a-daily_forecast
a-hourly_forecast
o-now
v-status : "ok"
o-suggestion
(注:a代表JsonArray,o代表JsonObject)
源码:
# -*- coding: UTF-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import urllib2 import json req = urllib2.Request('https://free-api.heweather.com/v5/weather?city=CN101010100&key=你的key&lang=en') response = urllib2.urlopen(req) f = json.loads(response.read()) print 'today weather天气:' print 'SO2:'+f['HeWeather5'][0]['aqi']['city']['so2'] print '穿衣指数:'+f['HeWeather5'][0]['suggestion']['drsg']['txt'] print '感冒指数:'+f['HeWeather5'][0]['suggestion']['flu']['txt']
备注:开始运行的时候,出现如下的错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 3: ordinal not in range(128)
然后加上了下面三行代码,成功解决问题:
import sys reload(sys) sys.setdefaultencoding('utf-8')
二、代码二
程序运行结果:
# -*- coding: utf-8 -*- __author__ = 'yvonne' import urllib2, json from datetime import date from os import path def get_city_id(cityname, search_type=1): ids = '' if search_type == 1: # 查询城市ID search = 'allchina' fn0 = 'cityID' elif search_type == 0: # 查询景点ID fn0 = 'viewID' search = 'allattractions' else: # 代表type值出错 return -1 fn = fn0 + str(date.today()) + '.txt' try: ''''' 查询是否存在当天的城市ID列表文件 若存在则直接读取文件里的信息 不存在则从接口再获取,景点也这么来 ''' if path.exists(fn): fp = open(fn, 'r') context = fp.read() fp.close() else: fp = open(fn, 'w') url_city = 'https://api.heweather.com/x3/citylist?search=' + search + '&key=请输入你的Key' req = urllib2.Request(url_city) resp = urllib2.urlopen(req) context = resp.read() # print True fp.write(context) fp.close() city_json = json.loads(context, encoding='utf-8') city_info = city_json["city_info"] city_name = unicode(cityname, 'utf-8') # print city_name for index, cities in enumerate(city_info): if city_name in cities['city']: # 暂时不考虑有多个的情况(因为数据是从前端传入而不是用户手动输入的) print cities['city'] ids = cities['id'] break else: # 之后将会返回-1 print '您输入的城市或景点不存在,请反馈给管理员' except IOError, e: print e else: pass return ids def get_city_weather(index, search_type=1): if search_type == 1: search = 'weather' elif search_type == 0: search = 'attractions' else: return -1 url_weather = 'https://api.heweather.com/x3/' + search + '?cityid=' + index + '&key=请输入你的Key' req = urllib2.Request(url_weather) resp = urllib2.urlopen(req) context = resp.read() weather_json = json.loads(context, encoding='utf-8') fp = open("test.txt", 'w') fp.write(context) fp.close() if search_type == 1: weather = weather_json["HeWeather data service 3.0"][0]['daily_forecast'][0] else: weather = weather_json return weather if __name__: # 测试代码 city = raw_input('请输入要查询的城市或景点:') city_id = get_city_id(city) print city_id city_weather = get_city_weather(city_id) print "最高温度", city_weather['tmp']['max'] print "最低温度", city_weather['tmp']['min'] print "日间天气", city_weather['cond']['txt_d'] print "夜间天气", city_weather['cond']['txt_n']
参考资料:Python天气预报
三、曲线图插件
https://www.highcharts.com/demo/
四、php代码
<?php //此程序为HeWeatherAPI演示使用,仅实现基本的JSON数据读取功能,供入门者PHP接口使用 //演示网站:三九天气在线 http://www.39ol.com/ //演示网站已实现CACHE缓存到本地,根据用户请求进行更新,一次请求格式整理供多个接口使用等功能 //QQ:19472686 header('Content-type:text/html;charset=utf-8'); //区域编号,演示程序默认使用北京 $areaid="CN101010100"; //HeWeatherAPI接口信息,请修改为自己申请的key $key="***"; //[函数]抓取远程网页内容 function curl_file_get_contents($url){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_TIMEOUT,5); curl_setopt($ch,CURLOPT_USERAGENT,_USERAGENT_); curl_setopt($ch,CURLOPT_REFERER,_REFERER_); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //支持HTTPS类型网页 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); $r=curl_exec($ch); curl_close($ch); return $r; } //[函数]将JSON数据转换为数组 function json_to_array($str){ if (is_string($str)) $str=json_decode($str); $arr=array(); foreach($str as $k=>$v){ if (is_object($v) || is_array($v)) $arr[$k]=json_to_array($v); else $arr[$k]=$v; } return $arr; } //使用函数抓取远程网页内容开始 $source=curl_file_get_contents('https://free-api.heweather.com/v5/weather?city='.$areaid.'&key='.$key.''); //将读取到的网页内容转换为JSON数据 $source_obj=json_decode($source); //以下三行程序语句在使用中可以依据json.cn等在线解析工具对JSON数据进行具体分析、具体对待 //获取HeWeather5对象 $public_source=$source_obj->HeWeather5; //使用函数将JSON数据转换为数组 $public_source=json_to_array($public_source); //取获取的数组第1个值 $public_source=$public_source[0]; //获取返回的数据状态 echo "数据状态:" . $public_source['status'] . " "; //获取发布时间 echo "更新时间:" . $public_source['basic']['update']['loc'] . " "; //获取天气预报 //第1天 echo $public_source['daily_forecast'][0]['date'] . ":" . $public_source['daily_forecast'][0]['cond']['code_d'] . "/" . $public_source['daily_forecast'][0]['cond']['txt_d'] . "/" . $public_source['daily_forecast'][0]['cond']['code_n'] . "/" . $public_source['daily_forecast'][0]['cond']['txt_n'] . "/" . $public_source['daily_forecast'][0]['tmp']['min'] . "℃~" . $public_source['daily_forecast'][0]['tmp']['max'] . "℃/" . $public_source['daily_forecast'][0]['wind']['dir'] . "/" . $public_source['daily_forecast'][0]['wind']['sc'] . " "; //第2天 echo $public_source['daily_forecast'][1]['date'] . ":" . $public_source['daily_forecast'][1]['cond']['code_d'] . "/" . $public_source['daily_forecast'][1]['cond']['txt_d'] . "/" . $public_source['daily_forecast'][1]['cond']['code_n'] . "/" . $public_source['daily_forecast'][1]['cond']['txt_n'] . "/" . $public_source['daily_forecast'][1]['tmp']['min'] . "℃~" . $public_source['daily_forecast'][1]['tmp']['max'] . "℃/" . $public_source['daily_forecast'][1]['wind']['dir'] . "/" . $public_source['daily_forecast'][1]['wind']['sc'] . " "; //第3天 echo $public_source['daily_forecast'][2]['date'] . ":" . $public_source['daily_forecast'][2]['cond']['code_d'] . "/" . $public_source['daily_forecast'][2]['cond']['txt_d'] . "/" . $public_source['daily_forecast'][2]['cond']['code_n'] . "/" . $public_source['daily_forecast'][2]['cond']['txt_n'] . "/" . $public_source['daily_forecast'][2]['tmp']['min'] . "℃~" . $public_source['daily_forecast'][2]['tmp']['max'] . "℃/" . $public_source['daily_forecast'][2]['wind']['dir'] . "/" . $public_source['daily_forecast'][2]['wind']['sc'] . " "; ?>