python查天气预报_Python编写一个天气预报查询系统

阅读: 评论:0

python查天⽓预报_Python编写⼀个天⽓预报查询系统
学了Python这么久 可以⽤它来做许多好玩的⼩程序哦! 这⾥给 ⼤家做了个查询天⽓预报的⼩程序! 可以查询未来⼏天的天⽓! 需要⼤家⾃⼰修改下代码即可!
⾸先我们需要建⽴⼀个字典dict ⽤来存放城市编码! 因为API接⼝是根据城市编码来查询的 ! 由于城市编码⽐较多 2600多⾏ 所有不在这⾥发了! 需要的可以⾃⾏百度去下载哦!
{"time":"2018-10-12 08:00:56","cityInfo":{"city":"北京市","cityId":"101010100","parent":"北
京","updateTime":"07:44"},"date":"20181012","message":"Success !","status":200,"data":
{"shidu":"74%","pm25":13.0,"pm10":45.0,"quality":"优","wendu":"5","ganmao":"各类⼈可⾃由活动","yesterday":{"date":"11⽇星期四","sunrise":"06:19","high":"⾼温 18.0℃","low":"低温 5.0℃","sunset":"17:43","aqi":36.0,"fx":"西南风","fl":"<3级","type":"晴","notice":"愿你拥有⽐阳光明媚的⼼情"},"forecast":[{"date":"12⽇星期五","sunrise":"06:20","high":"⾼温
20.0℃","low":"低温 8.0℃","sunset":"17:41","aqi":53.0,"fx":"南风","fl":"<3级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"}, {"date":"13⽇星期六","sunrise":"06:21","high":"⾼温 20.0℃","low":"低温 8.0℃","sunset":"17:40","aqi":73.0,"fx":"西南风","fl":"<3级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"14⽇星期⽇","sunrise":"06:22","high":"⾼温 22.0℃","low":"低温
10.0℃","sunset":"17:38","aqi":95.0,"fx":"西南风","fl":"<3级","type":"晴","notice":"愿你拥有⽐阳光明媚的⼼情"},{"date":"15⽇星期⼀","sunrise":"06:23","high":"⾼温 19.0℃","low":"低温 10.0℃","sunset":"17:37","aqi":61.0,"fx":"东北风","fl":"<3级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"16⽇星期⼆","sunrise":"06:24","high":"⾼温 20.0℃","low":"低温
9.0℃","sunset":"17:35","aqi":54.0,"fx":"北风","fl":"<3级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"}]}}
返回的是⼀个json 我们⽤ requests_html库 get请求即可 这个库是⾃带json()的!
好了 废话不多说了 我们还是直接上代码吧! 如有不⾜的地⽅欢迎指出!还请⼤神勿喷!
from requests_html import HTMLSession
from city import city
session = HTMLSession()
def weather(cs):
dm = city(cs)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
url = 't./api/weather/city/%s' % dm
r = (url, headers=headers)
#r.encoding = r.apparent_encoding
r = r.json()
try:
print(r['time'] + r['cityInfo']['city'])
for i in r['data']['forecast'][0]:
print(r['data']['forecast'][0][i])
except:
print('查询失败!请确认城市名称是否正确!')
if __name__ == '__main__':
while True:
print('*' * 7 + '⼩七天⽓预报查询!(输⼊0退出!)' + '*' * 7) cs = input('请输⼊城市名称: ')
if cs != '0':
weather(cs)
else:
print('欢迎再次使⽤!\n已经退出天⽓查询!')
break

本文发布于:2023-05-08 10:37:25,感谢您对本站的认可!

本文链接:http://www.035400.com/whly/1/388094.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:查询   城市   预报   确认   编码
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2024-2030 Comsenz Inc.Powered by © 文化旅游网 滇ICP备2022007236号-403 联系QQ:1103060800网站地图