猫史档案馆


秀个天气预报

用户:少年黑客神威少年黑客神威查看:0 回复:0 评论:0 创建时间:2022-11-19T16:28:59


#测试用压缩的数据

import re

from typing import List

from urllib.request import Request, urlopen

import gzip,zlib

import json,tkinter

 

#网站获取数据Api

url = 'http://t.weather.sojson.com/api/weather/city/101210401'

#包装头部

firefox_headers = {'User-Agent': " Mozilla/5.0 (Windows NT 6.1; Win喵; x喵; rv:70.0) Gecko/20100101 Firefox/70.0 " , "Accept-Encoding" :"gzip, deflate"}

 

#构建请求

request = Request( url, headers=firefox_headers )

html = urlopen( request )

 

 

 

#获取数据以utf-8的编码方式

#data2 = html.read().decode('utf-8')

data2 = zlib.decompress(html.read(), 16+zlib.MAX_WBITS).decode('utf-8')

data1=str(data2)

# 将json数据转换为dict数据

weather_dict=json.loads(data2)

#data3 =dict(html.read().decode('utf-8'))

#data3 =dict (str(data2))

 

#print(weather_dict)

forecast = weather_dict.get('data').get('forecast')

'''

print('城市:', weather_dict.get('cityInfo').get('parent')+' '+weather_dict.get('cityInfo').get('city'))

print('更新时间:', weather_dict.get('cityInfo').get('updateTime'))

print('温度:', weather_dict.get('data').get('wendu') + '℃ ')

print('空气质量:', weather_dict.get('data').get('quality'))

print('风向:', forecast[0].get('fx'))

print('风向:', weather_dict.get('data').get('forecast')[0].get('fx'))

'''

 

top=tkinter.Tk()

 

label1=tkinter.Label(top,text=('城市:', weather_dict.get('cityInfo').get('parent'),weather_dict.get('cityInfo').get('city')))

label2=tkinter.Label(top,text=('今日天气:温度',weather_dict.get('data').get('wendu'),'空气质量',weather_dict.get('data').get('quality'),'湿度',weather_dict.get('data').get('shidu')))

label3=tkinter.Label(top,text=('pm25',weather_dict.get('data').get('pm25'),'pm10',weather_dict.get('data').get('pm10'),'感冒指数',weather_dict.get('data').get('ganmao')))

 

label1.grid(row=0,column=0,padx=50,pady=10)

label2.grid(row=1,column=0,padx=50,pady=10)

label3.grid(row=2,column=0,padx=50,pady=10)

 

top.mainloop()

 


回复

上一页1 页 / 共 0下一页