-
Notifications
You must be signed in to change notification settings - Fork 4
/
xmlretriever.py
63 lines (39 loc) · 1.1 KB
/
xmlretriever.py
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
# -*- coding: utf-8 -*-
# With thanks to https://stackoverflow.com/users/6792743/perl
# import libraries
import pandas as pd
import requests
import xml.etree.ElementTree as ET
import io
from io import StringIO
import time
import datetime
# Get values for current year, month, and year
dt = datetime.datetime.today()
year = dt.year
month = dt.month
day = dt.day-1 # Variable day equals the previous day
print(year,month,day)
# Define XML query URL based on date
url = f"https://fogos.icnf.pt/localizador/webserviceocorrencias.asp" \
f"?ANO={year}" \
f"&MES={month}" \
f"&DIA={day}" \
# Get last update
df_actual=pd.read_csv("icnf_2022_raw.csv")
# XML Query
# Get data
resp = requests.get(url)
# Parse XML
et = ET.parse(io.StringIO(resp.text))
# Create DataFrame
df = pd.DataFrame([
{f.tag: f.text for f in e.findall('./')} for e in et.findall('./')]
)
df = df.reset_index()
#Append only new records
df_actual.append(df[df.isin(df_actual) == False])
df_sorted = df_actual.sort_values(["ANO","MES","DIA"])
df_sorted.reset_index()
# Save to CSV
df_sorted.to_csv("icnf_2022_raw.csv",index=False)