In [1]:
import requests as reqs
from requests.packages.urllib3.exceptions import InsecureRequestWarning
reqs.packages.urllib3.disable_warnings(InsecureRequestWarning)
In [2]:
sc="https://www.meteo.lv/josso_security_check"
sec_check=reqs.post(sc)
cookies=sec_check.cookies.get_dict()
cookies["JSESSIONID"]
Out[2]:
In [3]:
url="https://www.meteo.lv/meteorologija-datu-meklesana/?"
headers={
"Content-Type":"application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Referer": "https://www.meteo.lv/meteorologija-datu-meklesana/?nid=461",
"Cookie": "JSESSIONID="+cookies["JSESSIONID"]
}
In [4]:
import json
saraksts=reqs.get("https://www.meteo.lv/klasifikatoru-filtrs/?iBy=station&iStation=&iParameter=4001&pMonitoringType=METEOROLOGY")
saraksts= json.loads(saraksts.text)
In [5]:
stacijuSaraksts={}
for each in saraksts["stations"][1:]:
stacijuSaraksts[each["id"]]=each["name"]
parametruSaraksts={}
for each in saraksts["parameters"][1:]:
parametruSaraksts[each["id"]]=each["name"]
In [7]:
####
stationID=30022
paramID=4001
startYear=2016
endYear=2020
####
for year in range(startYear,endYear+1):
StartDate="01.01."+str(year)
EndDate="31.12."+str(year)
params="iBy=station&nid=461&pMonitoringType=METEOROLOGY&iStation="+str(stationID)+"&iParameter="+str(paramID)+"&iDateFrom="+StartDate+"&iDateTill="+EndDate
fname=stacijuSaraksts[str(stationID)]+"_" \
+parametruSaraksts[str(paramID)] + "_" \
+StartDate+"-" \
+EndDate+".xls"
print(fname)
result=reqs.post(url,verify=False,data=params, headers=headers)
with open(fname, 'wb') as f:
f.write(result.content)
In [8]:
import pandas as pd
In [12]:
####
stationID=30022
paramID=4001
startYear=2016
endYear=2020
####
df=[]
for enum, year in enumerate(range(startYear,endYear+1)):
StartDate="01.01."+str(year)
EndDate="31.12."+str(year)
fname=stacijuSaraksts[str(stationID)]+"_" \
+parametruSaraksts[str(paramID)] + "_" \
+StartDate+"-" \
+EndDate+".xls"
#df[enum]=pd.read_excel(fname)
df.append(pd.read_excel(fname,skiprows=1,parse_dates=["Datums \ Laiks"],index_col=0,dayfirst=True))
df[enum]["vidējā"]=df[enum].mean(axis=1)
#df[enum]["summa"]=df[enum].sum(axis=1)
In [13]:
dataf=pd.concat(df)
In [14]:
dataf
Out[14]:
In [16]:
dataf=dataf[["vidējā"]]
In [17]:
fname2=stacijuSaraksts[str(stationID)]+"_" \
+parametruSaraksts[str(paramID)] + "_" \
+str(startYear)+"-" \
+str(endYear)+".xls"
dataf.to_excel(fname2)
In [18]:
fname2
Out[18]:
In [ ]: