用海龟制图画出网格图以及温度变化图(高温部分)
#测试用压缩的数据
import re
from typing import List
from urllib.request import Request, urlopen
import gzip,zlib
import json,tkinter,turtle
#网站获取数据Api
url = 'http://t.weather.sojson.com/api/weather/city/101250101'
#包装头部
firefox_headers = {'User-Agent': " Mozilla/5.0 (Windows NT 6.1; Win64; x64; 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'))
'''
t_d = [weather_dict.get('data').get('yesterday').get('ymd')]
t_h = [int(str([weather_dict.get('data').get('yesterday').get('high')])[5:-3])]
#t_h = int(str(t_h1)[5:-3])
t_l = [int(str([weather_dict.get('data').get('yesterday').get('low')])[5:-3])]
for i in range(1,16,1):
t_d.insert(i, forecast[i - 1].get('ymd'))
t_h.insert(i, int(str([forecast[i - 1].get('high')])[5:-3]))
# t_h.insert(i, forecast[i - 1].get('high'))
t_l.insert(i, int(str([forecast[i - 1].get('low')])[5:-3]))
# t_l.insert(i, forecast[i - 1].get(str([weather_dict.get('data').get('yesterday').get('low' )])[3:-3]))
# t_l.insert(i, forecast[i - 1].get('low'))
'''
for i in range(0, 16, 1):
print(i)
print(t_d[i]+'\t'+str(t_h[i])+'\t'+str(t_l[i]))
print('\n')
'''
#海龟制图默认为350*350的方形
#绘制网格线
turtle.speed(0)
turtle.width(1)
turtle.color('black')
turtle.penup()
#更改起始位置
turtle.goto(-330, 300)
for x in range(0,51,1):
turtle.goto(-330, 330 - x * 13)
# turtle.color('red')
# turtle.write(50-x,font='bold')
# turtle.color('black')
turtle.pendown()
turtle.goto( 330, 330 - x * 13)
turtle.penup()
for y in range(0,17,1):
turtle.goto(-330+y*44, 330)
# turtle.color('red')
# turtle.write(t_d[y])
# turtle.color('black')
turtle.pendown()
turtle.goto(-330+y*44, -330)
turtle.penup()
#我以X轴-330为昨日的坐标位,330为15日后的坐标位
#我以Y轴-330为零下50°的坐标,330为正50度的坐标
turtle.speed(10)
turtle.width(10)
turtle.speed(10)
turtle.width(10)
turtle.color('green')
turtle.penup()
turtle.goto(-330,t_h[0]*6.6)
turtle.pendown()
turtle.color('red')
turtle.goto(-286,t_h[1]*6.6)
turtle.goto(-242,t_h[2]*6.6)
turtle.color('green')
turtle.goto(-198,t_h[3]*6.6)
turtle.goto(-154,t_h[4]*6.6)
turtle.goto(-110,t_h[5]*6.6)
turtle.color('green')
turtle.goto(-66,t_h[6]*6.6)
turtle.goto(-22,t_h[7]*6.6)
turtle.goto(22,t_h[8]*6.6)
turtle.goto(66,t_h[9]*6.6)
turtle.goto(110,t_h[10]*6.6)
turtle.goto(154,t_h[11]*6.6)
turtle.goto(198,t_h[12]*6.6)
turtle.goto(242,t_h[13]*6.6)
turtle.goto(286,t_h[14]*6.6)
turtle.goto(330,t_h[15]*6.6)
turtle.done()
#os.system('pause')
mainloop()