python 获取天气信息

2018-12-12  本文已影响42人  谢小帅

get_weather.py

import json, requests, pprint

weatherJsonUrl = "http://wthrcdn.etouch.cn/weather_mini?city=杭州"  # 将链接定义为一个字符串
response = requests.get(weatherJsonUrl)  # 获取并下载页面,其内容会保存在respons.text成员变量里面
response.raise_for_status()  # 这句代码的意思如果请求失败的话就会抛出异常,请求正常就上面也不会做

# 将json文件格式导入成python的格式
weatherData = json.loads(response.text)
pprint.pprint(weatherData)

weather_dict = dict()
weather_dict['high'] = weatherData['data']['forecast'][0]['high']
weather_dict['low'] = weatherData['data']['forecast'][0]['low']
weather_dict['type'] = weatherData['data']['forecast'][0]['type']
weather_dict['fengxiang'] = weatherData['data']['forecast'][0]['fengxiang']
weather_dict['ganmao'] = weatherData['data']['ganmao']
pprint.pprint(weather_dict)
上一篇下一篇

猜你喜欢

热点阅读