import re
import pandas as pd
import requests
import json
year = 2022
month = 5
url = f"https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D=59289&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}"
response = requests.get(url)
json_dict = json.loads(response.text)
data_str = json_dict['data']
print(data_str)
re_linestring = re.compile(r'\s<td>.*')
re_high= re.compile(r'\s<td style="color:#ff5040;">.*')
re_lower= re.compile(r'\s<td style="color:#3097fd;" >.*')
high = re.findall(re_high, data_str)
lower = re.findall(re_lower, data_str)
print(high, lower)
high = [i.replace(' <td style="color:#ff5040;">', "").replace("°</td>", '') for i in high]
high = list(map(int, high))
print(high)
lower = [i.replace(' <td style="color:#3097fd;" >', "").replace("°</td>", '') for i in lower]
lower = list(map(int, lower))
print(lower)
years = re.findall(re_linestring, data_str)
years = [i.replace(" <td>", "") for i in years]
print(years)
years = [i.split(" ")[0] for i in years]
print(years)
years = [i for i in years if "-" in i]
print(years)
df = pd.DataFrame({'years': years, 'high': high, 'lower': lower})
lis = []
for dic in df.to_dict("records"):
for i in range(24):
lis.append(dic)
new_df = pd.DataFrame(lis)
new_df.to_csv(f'{year}_{month}_new_df.csv', index=False)