OpenWeatherMap API

API

OpenWeather提供了多种不同的API,比如实时查询、5日预报、16日预报、历史数据、UV指数、污染指数、预警等等。本次作业需要的是实时查询,所以先从实时查询开始看起。

阅读了requests基础功能后,尝试使用

r = requests.get(http://api.openweathermap.org/data/2.5/weather?q=London)

返回401错误。

Getting APPID

意识到使用API,首先要有API Key

获得API Key [Signup] 4189fb56ae409dd072cbb67a4ed90901

payload = {'APPID':'4189fb56ae409dd072cbb67a4ed90901'}
r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London',
              params=payload)

** 使用该API注意事项

  1. 10分钟内不要重复发送请求,通常天气状态的变化不会那么频繁
  2. 使用api.openweathermap.org的服务器,不要用IP地址
  3. 通过城市的ID来调用,而不是城市名字、坐标或者邮编
  4. 免费账户有限制

实时查询

查询一个地理位置

可以通过城市名字、城市ID、地理坐标、邮编

城市名 api.openweathermap.org/data/2.5/weather?q=London api.openweathermap.org/data/2.5/weather?q=London,uk ID api.openweathermap.org/data/2.5/weather?id=2172797 城市ID列表 - 下载文件

地理坐标 api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}

邮编 api.openweathermap.org/data/2.5/weather?zip={zip code},{country code}

查询多个地理位置

长方形区域内、圆形区域内、多个城市IT(最多20)

批量下载

返回参数

天气状况图标

icons

作业相关问题

  1. 输入只有英文(拼音)才可以,尝试了中文“北京”、”上海", 均返回错误的结果“台湾” 需要将输入的中文转换成拼音才可以;单独写教程

  2. 查无此城市-r.status_code不是200

  3. 输出中文结果 API支持多种语言输出;将&lang=zh_cn加入查询URL即可。 但从结果上来看,只有r.json()['weather'][0]['description']返回中文,其余仍为英文。

Change log

2017.1.29 First Draft

results matching ""

    No results matching ""