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
| """ @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
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()
|