本程序仅用十几行代码,调用天气查询API,实现查询城市5日天气情况查询。
import requests
import json
weatherUrl = "http://wthrcdn.etouch.cn/weather_mini?city=" # 返回json数据
cityName = input("请输入你要查询的城市(如:北京):")
weatherResp = requests.get(weatherUrl + cityName)
d = weatherResp.json()
if d['status'] >= 1000:
print("城市:", d["data"]["city"])
for i in range(0, 5):
print("时间:", d["data"]["forecast"][i]["date"])
print("温度:", d["data"]["forecast"][i]["high"], d["data"]["forecast"][i]["low"])
print("天气:", d["data"]["forecast"][i]["type"])
print("-----------------------")