心知天气API
API使用步骤
注册API,获得appid和KEY
在接口中根据接口格式,获得相应数据
拆解数据,获得最终结果
接口数据
心知天气可以返回即时天气数据以及天气预报,在查看API文档时,发现接口还提供了天气对应的代码以及图标。180x180的图标放到GUI界面中,会非常美观啊,当时看到就想要在界面中把天气图标进行显示。

- 即时天气接口
接口url:
https://api.thinkpage.cn/v3/weather/now.json?key=cdvqctjafbrlflqo&location=beijing&language=zh-Hans&unit=c
返回数据:
{
"results": [{
"location": {
"id": "C23NB62W20TF",
"name": "西雅图",
"country": "US",
"timezone": "America/Los_Angeles",
"timezone_offset": "-07:00"
},
"now": {
"text": "多云", //天气现象文字
"code": "4", //天气现象代码
"temperature": "14", //温度,单位为c摄氏度或f华氏度
"feels_like": "14", //体感温度,单位为c摄氏度或f华氏度
"pressure": "1018", //气压,单位为mb百帕或in英寸
"humidity": "76", //相对湿度,0~100,单位为百分比
"visibility": "16.09", //能见度,单位为km公里或mi英里
"wind_direction": "西北", //风向文字
"wind_direction_degree": "340", //风向角度,范围0~360,0为正北,90为正东,180为正南,270为正西
"wind_speed": "8.05", //风速,单位为km/h公里每小时或mph英里每小时
"wind_scale": "2", //风力等级,请参考:http://baike.baidu.com/view/465076.htm
"clouds": "90", //云量,范围0~100,天空被云覆盖的百分比 #目前不支持中国城市#
"dew_point": "-12" //露点温度,请参考:http://baike.baidu.com/view/118348.htm #目前不支持中国城市#
},
"last_update": "2015-09-25T22:45:00-07:00" //数据更新时间(该城市的本地时间)
}]
}
接口文档中还说明:付费用户可获取全部数据,免费用户只返回天气现象文字、代码和气温3项数据。
所以在程序中,只取天气现象文字、代码和气温3项数据即可,其他的接口也不会返回给我。
- 天气预报接口
北京的昨日天气、今天和未来3天的预报:
https://api.thinkpage.cn/v3/weather/daily.json?key=cdvqctjafbrlflqo&location=beijing&language=zh-Hans&unit=c&start=-1&days=5
{
"results": [{
"location": {
"id": "WX4FBXXFKE4F",
"name": "北京",
"country": "CN",
"path": "北京,北京,中国",
"timezone": "Asia/Shanghai",
"timezone_offset": "+08:00"
},
"daily": [{ //返回指定days天数的结果
"date": "2015-09-20", //日期
"text_day": "多云", //白天天气现象文字
"code_day": "4", //白天天气现象代码
"text_night": "晴", //晚间天气现象文字
"code_night": "0", //晚间天气现象代码
"high": "26", //当天最高温度
"low": "17", //当天最低温度
"precip": "0", //降水概率,范围0~100,单位百分比
"wind_direction": "", //风向文字
"wind_direction_degree": "255", //风向角度,范围0~360
"wind_speed": "9.66", //风速,单位km/h(当unit=c时)、mph(当unit=f时)
"wind_scale": "" //风力等级
}, {
"date": "2015-09-21",
"text_day": "晴",
"code_day": "0",
"text_night": "晴",
"code_night": "0",
"high": "27",
"low": "17",
"precip": "0",
"wind_direction": "",
"wind_direction_degree": "157",
"wind_speed": "17.7",
"wind_scale": "3"
}, {
... //更多返回结果
}],
"last_update": "2015-09-20T18:00:00+08:00" //数据更新时间(该城市的本地时间)
}]
}
接口文档中也提到:付费用户可获取全部数据,免费用户只返回3天天气预报。
即自己的程序访问API时,只能返回今天、明天和后天的天气数据。所以访问url的参数start要设为0,days要设为3。
知道接口的访问方式和返回数据结构,就可以开始动手编程了!
接口调试
- 在浏览器中测试一下接口
在浏览器的地址栏直接输入示例URL:
https://api.thinkpage.cn/v3/weather/now.json?key=cdvqctjafbrlflqo&location=beijing&language=zh-Hans&unit=c
然后替换掉自己的key,获得API的返回结果:
{"results":[{"location":{"id":"WX4FBXXFKE4F","name":"北京","country":"CN","path":"北京,北京,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"多云","code":"4","temperature":"-4"},"last_update":"2017-01-29T09:15:00+08:00"}]}
这说明接口可以用,用程序使用相应的URL,并读取结果即可。
- CLI版本获取天气数据
程序中定义一个访问函数和一个拆解返回数据函数,完成接口的访问工作
def getWeatherInfo(city_input):
'''函数功能:到心知天气的API查询天气'''
url='https://api.thinkpage.cn/v3/weather/now.json'
params={'key':KEY, 'location':city_input,'language':'zh-Hans', 'unit':'c'}
r=requests.get(url, params=params)
return r
def decodeWeatherInfo(response_text):
'''函数功能:将json回应返回信息拆解'''
j=json.loads(response_text)
city_name=j['results'][0]['location']['name']
city_weather=j['results'][0]['now']['text']
city_temp=j['results'][0]['now']['temperature']
return city_name, city_weather, city_temp
结合CH1的程序,完成CLI版的程序:
G:\Python\py103\Chap3\project>python queryweather_online_v0.1.py
欢迎进入天气在线查询系统,本系统将到心知天气(http://www.thinkpage.cn/)查询相应的
城市天气信息。
请输入指令或您要查询的城市名:shenzhen
2017-01-22 16:37:20 深圳 的天气情况为:晴, 温度为:20℃
请输入指令或您要查询的城市名:吉林
2017-01-22 16:37:24 吉林 的天气情况为:多云, 温度为:-18℃
请输入指令或您要查询的城市名:齐齐哈尔
2017-01-22 16:37:30 齐齐哈尔 的天气情况为:多云, 温度为:-17℃
请输入指令或您要查询的城市名:三亚
2017-01-22 16:37:34 三亚 的天气情况为:多云, 温度为:25℃
请输入指令或您要查询的城市名:new york
你输入的指令错误或者城市名称不存在,请重新输入。
如果不清楚指令,可以输入 help,获取帮助。
请输入指令或您要查询的城市名:h
你查询过以下城市的天气情况:
1 2017-01-22 16:37:20 深圳 的天气情况为:晴, 温度为:20℃
2 2017-01-22 16:37:24 吉林 的天气情况为:多云, 温度为:-18℃
3 2017-01-22 16:37:30 齐齐哈尔 的天气情况为:多云, 温度为:-17℃
4 2017-01-22 16:37:34 三亚 的天气情况为:多云, 温度为:25℃
请输入指令或您要查询的城市名:q
你查询过以下城市的天气情况:
1 2017-01-22 16:37:20 深圳 的天气情况为:晴, 温度为:20℃
2 2017-01-22 16:37:24 吉林 的天气情况为:多云, 温度为:-18℃
3 2017-01-22 16:37:30 齐齐哈尔 的天气情况为:多云, 温度为:-17℃
4 2017-01-22 16:37:34 三亚 的天气情况为:多云, 温度为:25℃
感谢您的使用,再见!
G:\Python\py103\Chap3\project>
- 安装和调用requests
接口的示例代码中,是用requests来获取API响应的。而requests为第三方的库,需要手动安装。而自己安装的Anaconda3,默认安装了大部分的常用库,所以这里不需要再次安装。
如果需要安装的,可以用 pip3 install requests 进行安装。
测试是否有这个库的方法,可以在IPython中进行:
In [1]: import requests
In [2]:
如果import没有报错,requests就已经正确安装了。如果未安装的话,会提示"requests"名字未找到。
- 自己封装API,变为自己程序调用的格式
实现CLI调通后,自己重写了API,变为自己程序调用的格式。自己定义了查询即时天气和天气预报两个接口方法:
def get_weather_now(city_input):
"""Get realtime weather info from http://api.thinkpage.cn/
Args:
city_input: The name of the city input by user.
Returns:
dict_weather_now:{
"status_code": "200",
"message": "ok",
"name": city_name,
"weather": "city_weather",
"weather_code": "0",
"temp": "city_temp"
}
"""
def get_weather_forecast(city_input):
"""Get weather forecast from http://api.thinkpage.cn/
Args:
city_input: The name of the city input by user.
Returns:
dict_weather_forecast:{
"status_code": "200",
"message": "ok",
"name": city_name,
"date_d0": date_d0,
"weather_d0": city_weather_d0,
"weather_code_d0": weather_code_d0,
"temp_max_d0": temp_max_d0,
"temp_min_d0": temp_min_d0,
"date_d1": date_d1,
"weather_d1": city_weather_d1,
"weather_code_d1": weather_code_d1,
"temp_max_d1": temp_max_d1,
"temp_min_d1": temp_min_d1,
"date_d2": date_d2,
"weather_d2": city_weather_d2,
"weather_code_d2": weather_code_d2,
"temp_max_d2": temp_max_d2,
"temp_min_d2": temp_min_d2
}
"""
窗体程序中调用这两个方法,即可获得城市天气所需的参数。