代码拉取完成,页面将自动刷新
import requests, json
def getWeather(n):
"""
:return: 无锡天气情况
"""
if n == 0:
url = "https://www.sojson.com/open/api/weather/json.shtml?city=无锡"
html = requests.get(url).text # 天气数据 字符串类型
print("html", html)
jsonData = json.loads(html) # 字典类型
print("jsondata",jsonData)
data = jsonData['data']['forecast'] # 选取出来今天的数据
todayWeather = data[0]
date = todayWeather['date']
sunrise = todayWeather['sunrise']
high = todayWeather['high']
low = todayWeather['low']
weather = todayWeather['type']
notice = todayWeather['notice']
weatherData = {
'date': date,
'sunrise': sunrise,
'high': high,
'low': low,
'weather': weather,
'notice': notice
}
with open("weather.json", 'w', encoding='utf-8') as f:
f.write(json.dumps(weatherData))
else:
with open("weather.json", 'r', encoding='utf-8') as f:
weatherData = json.loads(f.read())
print("获取到天气", weatherData)
return weatherData
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。