# encoding: utf-8
# Author : Mr.Chen
# Datetime : 2020/9/15 16:06
# User : 31537
# Product : PyCharm
# Project : lianxi
# File : demo03_last.py
# explain : 查询天气
import time
import requests
while True:
city = input('请输入你要查询的城市:\n')
if not city:
break
req = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=%s' % city)
city_dic = req.json() # 装换为字典
city_data = city_dic.get('data')
if city_data:
forecast_data = city_data.get('forecast')
first_day = forecast_data[0]
scond_day = forecast_data[1]
third_day = forecast_data[2]
fourth_day = forecast_data[3]
fifth_day = forecast_data[4]
print('\n======以下为{} {}---- {} 的天气情况======\n'.format(city, first_day.get('date'), fifth_day.get('date')))
print('******************************************************')
print('{}:\n{}\t{}\n天气情况:{}'.format(first_day.get('date'), first_day.get('high'), first_day.get('low'),
first_day.get('type')))
print('******************************************************')
print('{}:\n{}\t{}\n天气情况:{}'.format(scond_day.get('date'), scond_day.get('high'), scond_day.get('low'),
scond_day.get('type')))
print('******************************************************')
print('{}:\n{}\t{}\n天气情况:{}'.format(third_day.get('date'), third_day.get('high'), third_day.get('low'),
third_day.get('type')))
print('******************************************************')
print('{}:\n{}\t{}\n天气情况:{}'.format(fourth_day.get('date'), fourth_day.get('high'), fourth_day.get('low'),
fourth_day.get('type')))
print('******************************************************')
print('{}:\n{}\t{}\n天气情况:{}'.format(fifth_day.get('date'), fifth_day.get('high'), fifth_day.get('low'),
fifth_day.get('type')))
print('******************************************************')
break
Python案例之查询天气
最新推荐文章于 2024-06-05 11:29:15 发布