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

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

2023年7月10日发(作者:)

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 HTMLSessionfrom city import citysession = 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.132Safari/537.36'}url = '/api/weather/city/%s' % dmr = (url, headers=headers)#ng = nt_encodingr = ()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

发布者:admin,转转请注明出处:http://www.yc00.com/web/1688932683a184983.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信