未来6天的天气分析

在这里需要主要的是,有两种格式解析。数据处理方式1和数据方式2,具体选哪一种,需要去网页看一下。

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
# -*- coding: utf-8 -*-
"""
@Time : 2022/12/31 21:24
@Author : daokunn
@File :九江.py
@IDE :PyCharm
@Motto: Don’t cry over spilt milk.

对未来6天预测数据可视化
"""
import requests
from bs4 import BeautifulSoup
from matplotlib import pyplot as plt
import pandas
import sys
import io

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

def bijie_weather():
# print('毕节')
res = requests.get('http://www.weather.com.cn/weather/101240201.shtml')
res.encoding = 'utf-8'
html = res.text
soup = BeautifulSoup(html,'html.parser')#解析文档
weathers = soup.find(id="7d",class_="c7d").find('ul',class_="t clearfix").find_all('li')

# result = {}
dates, conditions, temp = [], [], []
for weather in weathers:
weather_date = weather.find('h1')
weather_wea = (weather.find('p',class_='wea'))
weather_tem = (weather.find('p',class_= 'tem'))

# result['日期'] = weather_date.text
# result['天气'] = weather_wea.text
# result['温度'] = weather_tem.text[1:-1]
dates.append(weather_date.text)
conditions.append(weather_wea.text)
temp.append(weather_tem.text[1:-1])


# 数据保存
_data = pandas.DataFrame()
_data['日期'] = dates
_data['天气情况'] = conditions
_data['气温'] = temp
_data.to_csv('九江.csv', index=False, encoding='gbk')


# print(result)
# print('日期:',result['日期'])

# return result
return _data
# print(bijie_weather())

def draw():
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

datalsit = pandas.read_csv('九江.csv', encoding='gbk')
datalist = datalsit.drop([0])
print(datalist)




# 数据处理1,当格式为5℃/7℃时候使用
# datalist['最高气温'] = datalist['气温'].str.split('℃', expand=True)[0]
# datalist['最低气温'] = datalist['气温'].str.split('℃', expand=True)[1]
#
#
# datalist['最高气温'] = datalist['最高气温'].map(lambda x: int(x))
#
# datalist['最低气温'] = datalist['最低气温'].map(lambda x: int(str(x)[1:]))
# datalist['最低气温'] = datalist['最低气温'].map(lambda x: int(x))

# 数据处理方式2,当格式为 5/-1℃时候使用
datalist['最高气温'] = datalist['气温'].str.split('/', expand=True)[0]
datalist['最低气温'] = datalist['气温'].str.split('/', expand=True)[1]
datalist['最高气温'] = datalist['最高气温'].map(lambda x: int(x))
datalist['最低气温'] = datalist['最低气温'].map(lambda x: int(str(x)[:-1]))


print(datalist['最低气温'])



dates = datalist['日期']
highs = datalist['最高气温']
lows = datalist['最低气温']

# 画图

fig = plt.figure(dpi=128, figsize=(10, 6))

plt.plot(dates, highs, c='red', alpha=0.5)
plt.plot(dates, lows, c='blue', alpha=0.5)

plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.2)
# 图表格式
# 设置图标的图形格式
plt.title('未来6天九江市天气情况', fontsize=24)
plt.xlabel('', fontsize=6)
fig.autofmt_xdate()
plt.ylabel('气温', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=10)
# 修改刻度
plt.xticks(dates[::1])
# 显示
plt.show()

if __name__ == '__main__':
bijie_weather()
draw()

image-20230101102859433

历史天气分析

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
# -*- coding: utf-8 -*-
"""
@Time : 2022/12/31 21:02
@Author : daokunn
@File :weather.py
@IDE :PyCharm
@Motto: Don’t cry over spilt milk.
对10-12月进行分析
"""

import requests
from bs4 import BeautifulSoup
import io
import sys
import pandas
from matplotlib import pyplot as plt

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')


def get_data(weather_url):
rseponse = requests.get(weather_url)

html = rseponse.content.decode('gbk')
soup = BeautifulSoup(html, 'html.parser')

tr_lsit = soup.find_all('tr')

print(tr_lsit)
dates, conditions, temp = [], [], []
for data in tr_lsit[1:]:
sub_data = data.text.split()
dates.append(sub_data[0])
conditions.append(''.join(sub_data[1:3]))
temp.append(''.join(sub_data[3:6]))

# 数据保存
_data = pandas.DataFrame()
_data['日期'] = dates
_data['天气情况'] = conditions
_data['气温'] = temp
_data.to_csv('city.csv', index=False, encoding='gbk')

return _data
# print(_data)



data_month_10 =get_data('http://www.tianqihoubao.com/lishi/jiujiang/month/202210.html')
data_month_11 =get_data('http://www.tianqihoubao.com/lishi/jiujiang/month/202211.html')
data_month_12 =get_data('http://www.tianqihoubao.com/lishi/jiujiang/month/202212.html')

data = pandas.concat([data_month_10,data_month_11,data_month_12]).reset_index(drop=True)
data.to_csv('city.csv',index=False,encoding='gbk')


plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

datalsit = pandas.read_csv('city.csv', encoding='gbk')

# 数据处理
datalsit['最高气温'] = datalsit['气温'].str.split('/', expand=True)[0]
datalsit['最低气温'] = datalsit['气温'].str.split('/', expand=True)[1]

datalsit['最高气温'] = datalsit['最高气温'].map(lambda x: int(x.replace('℃', '')))
datalsit['最低气温'] = datalsit['最低气温'].map(lambda x: int(x.replace('℃', '')))


dates = datalsit['日期']
highs = datalsit['最高气温']
lows = datalsit['最低气温']

# 画图

fig = plt.figure(dpi=128, figsize=(10, 6))

plt.plot(dates, highs, c='red', alpha=0.5)
plt.plot(dates, lows, c='blue', alpha=0.5)

plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.2)
# 图表格式
# 设置图标的图形格式
plt.title('2022九江市10-12月天气情况', fontsize=24)
plt.xlabel('', fontsize=6)
fig.autofmt_xdate()
plt.ylabel('气温', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=10)
# 修改刻度
plt.xticks(dates[::20])
# 显示
plt.show()

image-20230101103632058