首页 > 编程语言 > 详细

python项目 - 查询城市天气预报

时间:2021-05-25 22:31:40      阅读:22      评论:0      收藏:0      [点我收藏+]

实现效果如下图所示

技术分享图片

附原码

使用爬虫实现

import requests

city = {‘北京‘: ‘101010100‘,
        ‘济南‘: ‘101120101‘,
        ‘青岛‘: ‘101120201‘,
        ‘淄博‘: ‘101120301‘,
        ‘烟台‘: ‘101120501‘,
        ‘潍坊‘: ‘101120601‘,
        ‘曲阜‘: ‘101120710‘,
        ‘泰安‘: ‘101120801‘,
        ‘滨州‘: ‘101121101‘}

cityname = input(‘输入查询城市:‘)
citycode = city[cityname]
url = ‘http://wthrcdn.etouch.cn/weather_mini?citykey=%s‘ % citycode
# print(‘城市:{},编号:{}‘.format(cityname,citycode))
# print(url)

res = requests.get(url)
info = res.json()
# print(info)
data = info[‘data‘]
weatherItems = data[‘forecast‘]
today = weatherItems[0]
city = f"【城市:{data[‘city‘]}】\n"
date = f"日期:{today[‘date‘]}\n"
now = f"实时温度:{data[‘wendu‘]}度\n"
tips = f"今日贴士:{data[‘ganmao‘]}\n"
print(city, date, now, tips)

print(‘【未来4日天气情况:】\n‘)
for today in weatherItems[1:]:
    # today = data[‘forecast‘][0]
    date = f"日期:{today[‘date‘]}"
    temperature = f"温度:{today[‘high‘]} {today[‘low‘]}"
    weatherType = f"天气:{today[‘type‘]}"
    print(‘{}, {}, {}\n‘.format(date, weatherType, temperature))

python项目 - 查询城市天气预报

原文:https://www.cnblogs.com/zhuhukang/p/14810316.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!