分析可以发现这一天西南风最多,平均风级达到了1.75级,东北风也有小部分1.0级,其余空白方向无来风。
未来14天高低温变化曲线图
统计未来14天的高低温度变化,并绘制出他们的变化曲线图,分别用虚线将他们的平均气温线绘制出来,最后结果如下所示:
分析可以发现未来14天高温平均气温为30.5℃,温度还是比较高,但是未来的第8天有降温,需要做好降温准备,低温前面处于平稳趋势,等到第8天开始下降,伴随着高温也下降,整体温度下降,低温平均在27℃左右。
未来14天风向风级雷达图
统计未来14天的风向和平均风力,并和前面一样采用极坐标形式,将圆周分为8个部分,代表8个方向,颜色越深代表风级越高,最后结果如下所示:
分析可以发现未来14天东南风、西南风所占主要风向,风级最高达到了5级,最低的西风平均风级也有3级。
未来14天气候分布饼图
统计未来14天的气候,并求每个气候的总天数,最后将各个气候的饼图绘制出来,结果如下所示:
分析可以发现未来14天气候基本是“雨”、“阴转雨”和“阵雨”,下雨的天数较多,结合前面的气温分布图可以看出在第8-9天气温高温下降,可以推测当天下雨,导致气温下降。
3、结论
1.首先根据爬取的温湿度数据进行的分析,温度从早上低到中午高再到晚上低,湿度和温度的趋势相反,通过相关系数发现温度和湿度有强烈的负相关关系,经查阅资料发现因为随着温度升高水蒸汽蒸发加剧,空气中水分降低湿度降低。当然,湿度同时受气压和雨水的影响,下雨湿度会明显增高。
2.经查阅资料空气质量不仅跟工厂、汽车等排放的烟气、废气等有关,更为重要的是与气象因素有关。由于昼夜温差明显变化,当地面温度高于高空温度时,空气上升,污染物易被带到高空扩散;当地面温度低于一定高度的温度时,天空形成逆温层,它像一个大盖子一样压在地面上空,使地表空气中各种污染物不易扩散。一般在晚间和清晨影响较大,而当太阳出来后,地面迅速升温,逆温层就会逐渐消散,于是污染空气也就扩散了。
3.风是由气压在水平方向分布的不均匀导致的。风受大气环流、地形、水域等不同因素的综合影响,表现形式多种多样,如季风、地方性的海陆风、山谷风等,一天的风向也有不同的变化,根据未来14天的风向雷达图可以发现未来所有风向基本都有涉及,并且没有特别的某个风向,原因可能是近期没有降水和气文变化不大,导致风向也没有太大的变化规律。
4.天气是指某一个地区距离地表较近的大气层在短时间内的具体状态。跟某瞬时内大气中各种气象要素分布的综合表现。根据未来14天的天气和温度变化可以大致推断出某个时间的气候,天气和温度之间也是有联系的。
4、代码框架
代码主要分为weather.py:对中国天气网进行爬取天气数据并保存csv文件;data1_analysis.py:对当天的天气信息进行可视化处理;data14_analysis.py:对未来14天的天气信息进行可视化处理。下面是代码的结构图:
附源代码
#
import requests
from bs4 import BeautifulSoup
import csv
import json
def getHTMLtext(url):
"""请求获得网页内容"""
try:
r = (url, timeout = 30)
r.raise_for_status()
= r.apparent_encoding
print("成功访问")
return
except:
print("访问错误")
return" "
def get_content(html):
"""处理得到有用信息保存数据文件"""
final = [] # 初始化一个列表保存数据
bs = BeautifulSoup(html, "") # 创建BeautifulSoup对象
body =
data = ('div', {<!-- -->'id': '7d'}) # 找到div标签且id = 7d
# 下面爬取当天的数据
data2 = body.find_all('div',{<!-- -->'class':'left-div'})
text = data2[2].find('script').string
text = text[('=')+1 :-2] # 移除改var data=将其变为json数据
jd = (text)
dayone = jd['od']['od2'] # 找到当天的数据
final_day = [] # 存放当天的数据
count = 0
for i in dayone:
temp = []
if count <=23:
(i['od21']) # 添加时间
(i['od22']) # 添加当前时刻温度
(i['od24']) # 添加当前时刻风力方向
(i['od25']) # 添加当前时刻风级
(i['od26']) # 添加当前时刻降水量
(i['od27']) # 添加当前时刻相对湿度
(i['od28']) # 添加当前时刻控制质量
#print(temp)
final_day.append(temp)
count = count +1
# 下面爬取7天的数据
ul = ('ul') # 找到所有的ul标签
li = ul.find_all('li') # 找到左右的li标签
i = 0 # 控制爬取的天数
for day in li: # 遍历找到的每一个li
if i < 7 and i > 0:
temp = [] # 临时存放每天的数据
date = ('h1').string # 得到日期
date = date[0:('日')] # 取出日期号
(date)
inf = day.find_all('p') # 找出li下面的p标签,提取第一个p标签的值,即天气
(inf[0].string)
tem_low = inf[1].find('i').string # 找到最低气温
if inf[1].find('span') is None: # 天气预报可能没有最高气温
tem_high = None
else:
tem_high = inf[1].find('span').string # 找到最高气温
(tem_low[:-1])
if tem_high[-1] == '℃':
(tem_high[:-1])
else:
(tem_high)
wind = inf[2].find_all('span') # 找到风向
for j in wind:
(j['title'])
wind_scale = inf[2].find('i').string # 找到风级
index1 = wind_scale.index('级')
(int(wind_scale[index1-1:index1]))
(temp)
i = i + 1
return final_day,final
#print(final)
def get_content2(html):
"""处理得到有用信息保存数据文件"""
final = [] # 初始化一个列表保存数据
bs = BeautifulSoup(html, "") # 创建BeautifulSoup对象
body =
data = ('div', {<!-- -->'id': '15d'}) # 找到div标签且id = 15d
ul = ('ul') # 找到所有的ul标签
li = ul.find_all('li') # 找到左右的li标签
final = []
i = 0 # 控制爬取的天数
for day in li: # 遍历找到的每一个li
if i < 8:
temp = [] # 临时存放每天的数据
date = ('span',{<!-- -->'class':'time'}).string # 得到日期
date = date[('(')+1:-2] # 取出日期号
(date)
weather = ('span',{<!-- -->'class':'wea'}).string # 找到天气
(weather)
tem = ('span',{<!-- -->'class':'tem'}).text # 找到温度
(tem[('/')+1:-1]) # 找到最低气温
(tem[:('/')-1]) # 找到最高气温
wind = ('span',{<!-- -->'class':'wind'}).string # 找到风向
if '转' in wind: # 如果有风向变化
(wind[:('转')])
(wind[('转')+1:])
else: # 如果没有风向变化,前后风向一致
(wind)
(wind)
wind_scale = ('span',{<!-- -->'class':'wind1'}).string # 找到风级
index1 = wind_scale.index('级')
(int(wind_scale[index1-1:index1]))
(temp)
return final
def write_to_csv(file_name, data, day=14):
"""保存为csv文件"""
with open(file_name, 'a', errors='ignore', newline='') as f:
if day == 14:
header = ['日期','天气','最低气温','最高气温','风向1','风向2','风级']
else:
header = ['小时','温度','风力方向','风级','降水量','相对湿度','空气质量']
f_csv = (f)
f_csv.writerow(header)
f_csv.writerows(data)
def main():
"""主函数"""
print("Weather test")
# 珠海
url1 = '/weather/' # 7天天气中国天气网
url2 = '/weather15d/' # 8-15天天气中国天气网
html1 = getHTMLtext(url1)
data1, data1_7 = get_content(html1) # 获得1-7天和当天的数据
html2 = getHTMLtext(url2)
data8_14 = get_content2(html2) # 获得8-14天数据
data14 = data1_7 + data8_14
#print(data)
write_to_csv('',data14,14) # 保存为csv文件
write_to_csv('',data1,1)
if __name__ == '__main__':
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
data1_analysis.py:
# data1_analysis.py
import as plt
import numpy as np
import pandas as pd
import math
def tem_curve(data):
"""温度曲线绘制"""
hour = list(data['小时'])
tem = list(data['温度'])
for i in range(0,24):
if (tem[i]) == True:
tem[i] = tem[i-1]
tem_ave = sum(tem)/24 # 求平均温度
tem_max = max(tem)
tem_max_hour = hour[(tem_max)] # 求最高温度
tem_min = min(tem)
tem_min_hour = hour[(tem_min)] # 求最低温度
x = []
y = []
for i in range(0, 24):
(i)
(tem[(i)])
(1)
(x,y,color='red',label='温度') # 画出温度曲线
(x,y,color='red') # 点出每个时刻的温度点
([0, 24], [tem_ave, tem_ave], c='blue', linestyle='--',label='平均温度') # 画出平均温度虚线
(tem_max_hour+0.15, tem_max+0.15, str(tem_max), ha='center', va='bottom', fontsize=10.5) # 标出最高温度
(tem_min_hour+0.15, tem_min+0.15, str(tem_min), ha='center', va='bottom', fontsize=10.5) # 标出最低温度
(x)
()
('一天温度变化曲线图')
('时间/h')
('摄氏度/℃')
()
def hum_curve(data):
"""相对湿度曲线绘制"""
hour = list(data['小时'])
hum = list(data['相对湿度'])
for i in range(0,24):
if (hum[i]) == True:
hum[i] = hum[i-1]
hum_ave = sum(hum)/24 # 求平均相对湿度
hum_max = max(hum)
hum_max_hour = hour[(hum_max)] # 求最高相对湿度
hum_min = min(hum)
hum_min_hour = hour[(hum_min)] # 求最低相对湿度
x = []
y = []
for i in range(0, 24):
(i)
(hum[(i)])
(2)
(x,y,color='blue',label='相对湿度') # 画出相对湿度曲线
(x,y,color='blue') # 点出每个时刻的相对湿度
([0, 24], [hum_ave, hum_ave], c='red', linestyle='--',label='平均相对湿度') # 画出平均相对湿度虚线
(hum_max_hour+0.15, hum_max+0.15, str(hum_max), ha='center', va='bottom', fontsize=10.5) # 标出最高相对湿度
(hum_min_hour+0.15, hum_min+0.15, str(hum_min), ha='center', va='bottom', fontsize=10.5) # 标出最低相对湿度
(x)
()
('一天相对湿度变化曲线图')
('时间/h')
('百分比/%')
()
def air_curve(data):
"""空气质量曲线绘制"""
hour = list(data['小时'])
air = list(data['空气质量'])
print(type(air[0]))
for i in range(0,24):
if (air[i]) == True:
air[i] = air[i-1]
air_ave = sum(air)/24 # 求平均空气质量
air_max = max(air)
air_max_hour = hour[(air_max)] # 求最高空气质量
air_min = min(air)
air_min_hour = hour[(air_min)] # 求最低空气质量
x = []
y = []
for i in range(0, 24):
(i)
(air[(i)])
(3)
for i in range(0,24):
if y[i] <= 50:
(x[i],y[i],color='lightgreen',width=0.7) # 1等级
elif y[i] <= 100:
(x[i],y[i],color='wheat',width=0.7) # 2等级
elif y[i] <= 150:
(x[i],y[i],color='orange',width=0.7) # 3等级
elif y[i] <= 200:
(x[i],y[i],color='orangered',width=0.7) # 4等级
elif y[i] <= 300:
(x[i],y[i],color='darkviolet',width=0.7) # 5等级
elif y[i] > 300:
(x[i],y[i],color='maroon',width=0.7) # 6等级
([0, 24], [air_ave, air_ave], c='black', linestyle='--') # 画出平均空气质量虚线
(air_max_hour+0.15, air_max+0.15, str(air_max), ha='center', va='bottom', fontsize=10.5) # 标出最高空气质量
(air_min_hour+0.15, air_min+0.15, str(air_min), ha='center', va='bottom', fontsize=10.5) # 标出最低空气质量
(x)
('一天空气质量变化曲线图')
('时间/h')
('空气质量指数AQI')
()
def wind_radar(data):
"""风向雷达图"""
wind = list(data['风力方向'])
wind_speed = list(data['风级'])
for i in range(0,24):
if wind[i] == "北风":
wind[i] = 90
elif wind[i] == "南风":
wind[i] = 270
elif wind[i] == "西风":
wind[i] = 180
elif wind[i] == "东风":
wind[i] = 360
elif wind[i] == "东北风":
wind[i] = 45
elif wind[i] == "西北风":
wind[i] = 135
elif wind[i] == "西南风":
wind[i] = 225
elif wind[i] == "东南风":
wind[i] = 315
degs = (45,361,45)
temp = []
for deg in degs:
speed = []
# 获取 wind_deg 在指定范围的风速平均值数据
for i in range(0,24):
if wind[i] == deg:
(wind_speed[i])
if len(speed) == 0:
(0)
else:
(sum(speed)/len(speed))
print(temp)
N = 8
theta = (0.+/8,2*+/8,2*/8)
# 数据极径
radii = (temp)
# 绘制极区图坐标系
(polar=True)
# 定义每个扇区的RGB值(R,G,B),x越大,对应的颜色越接近蓝色
colors = [(1-x/max(temp), 1-x/max(temp),0.6) for x in radii]
(theta,radii,width=(2*/N),bottom=0.0,color=colors)
('一天风级图',x=0.2,fontsize=20)
()
def calc_corr(a, b):
"""计算相关系数"""
a_avg = sum(a)/len(a)
b_avg = sum(b)/len(b)
cov_ab = sum([(x - a_avg)*(y - b_avg) for x,y in zip(a, b)])
sq = (sum([(x - a_avg)**2 for x in a])*sum([(x - b_avg)**2 for x in b]))
corr_factor = cov_ab/sq
return corr_factor
def corr_tem_hum(data):
"""温湿度相关性分析"""
tem = data['温度']
hum = data['相对湿度']
(tem,hum,color='blue')
("温湿度相关性分析图")
("温度/℃")
("相对湿度/%")
(20,40,"相关系数为:"+str(calc_corr(tem,hum)),fontdict={<!-- -->'size':'10','color':'red'})
()
print("相关系数为:"+str(calc_corr(tem,hum)))
def main():
['-serif']=['SimHei'] # 解决中文显示问题
['axes.unicode_minus'] = False # 解决负号显示问题
data1 = pd.read_csv('',encoding='gb2312')
print(data1)
tem_curve(data1)
hum_curve(data1)
air_curve(data1)
wind_radar(data1)
corr_tem_hum(data1)
if __name__ == '__main__':
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
data14_analysis.py:
# data14_analysis.py
import as plt
import numpy as np
import pandas as pd
import math
def tem_curve(data):
"""温度曲线绘制"""
date = list(data['日期'])
tem_low = list(data['最低气温'])
tem_high = list(data['最高气温'])
for i in range(0,14):
if (tem_low[i]) == True:
tem_low[i] = tem_low[i-1]
if (tem_high[i]) == True:
tem_high[i] = tem_high[i-1]
tem_high_ave = sum(tem_high)/14 # 求平均高温
tem_low_ave = sum(tem_low)/14 # 求平均低温
tem_max = max(tem_high)
tem_max_date = tem_high.index(tem_max) # 求最高温度
tem_min = min(tem_low)
tem_min_date = tem_low.index(tem_min) # 求最低温度
x = range(1,15)
(1)
(x,tem_high,color='red',label='高温') # 画出高温度曲线
(x,tem_high,color='red') # 点出每个时刻的温度点
(x,tem_low,color='blue',label='低温') # 画出低温度曲线
(x,tem_low,color='blue') # 点出每个时刻的温度点
([1, 15], [tem_high_ave, tem_high_ave], c='black', linestyle='--') # 画出平均温度虚线
([1, 15], [tem_low_ave, tem_low_ave], c='black', linestyle='--') # 画出平均温度虚线
()
(tem_max_date+0.15, tem_max+0.15, str(tem_max), ha='center', va='bottom', fontsize=10.5) # 标出最高温度
(tem_min_date+0.15, tem_min+0.15, str(tem_min), ha='center', va='bottom', fontsize=10.5) # 标出最低温度
(x)
('未来14天高温低温变化曲线图')
('未来天数/天')
('摄氏度/℃')
()
def change_wind(wind):
"""改变风向"""
for i in range(0,14):
if wind[i] == "北风":
wind[i] = 90
elif wind[i] == "南风":
wind[i] = 270
elif wind[i] == "西风":
wind[i] = 180
elif wind[i] == "东风":
wind[i] = 360
elif wind[i] == "东北风":
wind[i] = 45
elif wind[i] == "西北风":
wind[i] = 135
elif wind[i] == "西南风":
wind[i] = 225
elif wind[i] == "东南风":
wind[i] = 315
return wind
def wind_radar(data):
"""风向雷达图"""
wind1 = list(data['风向1'])
wind2 = list(data['风向2'])
wind_speed = list(data['风级'])
wind1 = change_wind(wind1)
wind2 = change_wind(wind2)
degs = (45,361,45)
temp = []
for deg in degs:
speed = []
# 获取 wind_deg 在指定范围的风速平均值数据
for i in range(0,14):
if wind1[i] == deg:
(wind_speed[i])
if wind2[i] == deg:
(wind_speed[i])
if len(speed) == 0:
(0)
else:
(sum(speed)/len(speed))
print(temp)
N = 8
theta = (0.+/8,2*+/8,2*/8)
# 数据极径
radii = (temp)
# 绘制极区图坐标系
(polar=True)
# 定义每个扇区的RGB值(R,G,B),x越大,对应的颜色越接近蓝色
colors = [(1-x/max(temp), 1-x/max(temp),0.6) for x in radii]
(theta,radii,width=(2*/N),bottom=0.0,color=colors)
('未来14天风级图',x=0.2,fontsize=20)
()
def weather_pie(data):
"""绘制天气饼图"""
weather = list(data['天气'])
dic_wea = {<!-- --> }
for i in range(0,14):
if weather[i] in dic_wea.keys():
dic_wea[weather[i]] += 1
else:
dic_wea[weather[i]] = 1
print(dic_wea)
explode=[0.01]*len(dic_wea.keys())
color = ['lightskyblue','silver','yellow','salmon','grey','lime','gold','red','green','pink']
(dic_wea.values(),explode=explode,labels=dic_wea.keys(),autopct='%1.1f%%',colors=color)
('未来14天气候分布饼图')
()
def main():
['-serif']=['SimHei'] # 解决中文显示问题
['axes.unicode_minus'] = False # 解决负号显示问题
data14 = pd.read_csv('',encoding='gb2312')
print(data14)
tem_curve(data14)
wind_radar(data14)
weather_pie(data14)
if __name__ == '__main__':
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
还有兄弟不知道网络安全面试可以提前刷题吗?费时一周整理的160+网络安全面试题,金九银十,做网络安全面试里的显眼包!
王岚嵚工程师面试题(附答案),只能帮兄弟们到这儿了!如果你能答对70%,找一个安全工作,问题不大。
对于有1-3年工作经验,想要跳槽的朋友来说,也是很好的温习资料!
【完整版领取方式在文末!!】
93道网络安全面试题
内容实在太多,不一一截图了
黑客学习资源推荐
最后给大家分享一份全套的网络安全学习资料,给那些想学习 网络安全的小伙伴们一点帮助!
对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。
????朋友们如果有需要的话,可以联系领取~
1️⃣零基础入门
① 学习路线
对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。
② 路线对应学习视频
同时每个成长路线对应的板块都有配套的视频提供:
2️⃣视频配套工具&国内外网安书籍、文档
① 工具
② 视频
③ 书籍
资源较为敏感,未展示全面,需要的最下面获取
② 简历模板
因篇幅有限,资料较为敏感仅展示部分资料,添加上方即可获取????
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化资料的朋友,可以点击这里获取
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!