Weather Forecast

Python : Document

Grab Weather Forecast with Python & Api

Code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*-
import requests
import json
import pickle

pickle_file = open('city_list.pkl', 'rb')
city = pickle.load(pickle_file)

password=input('请输入城市:')
name1=city[password]

#url = 'http://wthrcdn.etouch.cn/weather_mini?citykey=' + name1
jsonStr = requests.get(r'http://wthrcdn.etouch.cn/weather_mini?citykey=' + name1).text

data = json.loads(jsonStr)
weather = data["data"]

print("city:", weather["city"])
print("prompt:", weather["ganmao"])
for x in weather["forecast"]:
print(x["date"], x["type"], x["high"], x["low"], x["fengxiang"], x["fengli"])

Result :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
请输入城市:苏州
city: 苏州
prompt: 各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。
17日星期三 多云 高温 35℃ 低温 28℃ 东风 3-4级
18日星期四 晴 高温 36℃ 低温 28℃ 东南风 3-4级
19日星期五 晴 高温 37℃ 低温 29℃ 东南风 3-4级
20日星期六 多云 高温 37℃ 低温 28℃ 南风 3-4级
21日星期天 雷阵雨 高温 34℃ 低温 29℃ 东北风 3-4级

请输入城市:北京
city: 北京
prompt: 各项气象条件适宜,无明显降温过程,发生感冒机率较低。
17日星期三 阵雨 高温 28℃ 低温 22℃ 无持续风向 微风级
18日星期四 大雨 高温 25℃ 低温 22℃ 无持续风向 微风级
19日星期五 多云 高温 28℃ 低温 23℃ 无持续风向 微风级
20日星期六 晴 高温 30℃ 低温 23℃ 无持续风向 微风级
21日星期天 晴 高温 32℃ 低温 22℃ 无持续风向 微风级

请输入城市:哈尔滨
city: 哈尔滨
prompt: 各项气象条件适宜,无明显降温过程,发生感冒机率较低。
17日星期三 晴 高温 28℃ 低温 18℃ 西风 微风级
18日星期四 多云 高温 28℃ 低温 18℃ 南风 微风级
19日星期五 多云 高温 27℃ 低温 17℃ 东南风 微风级
20日星期六 多云 高温 27℃ 低温 17℃ 东南风 3-4级
21日星期天 雷阵雨 高温 24℃ 低温 15℃ 东风 3-4级

注意此处使用的城市代码来源于国家气象局

[转载]API及全国地区代码


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!