Python案例之查询天气

# 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


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值