You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.0 KiB
Python

2 years ago
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2023 LEdoian <volby@pokemon.ledoian.cz>
# SPDX-License-Identifier: GPL-2.0-only
import requests
import xml.etree.ElementTree as ET
from io import StringIO
from time import sleep
from pathlib import Path
URL=r'https://www.volby.cz/pls/prez2023/vysledky_okrsky?kolo=1&davka='
def repeat_get(url):
while True:
try:
return requests.get(url)
except OSError:
print('fuu')
sleep(1)
continue
# Kolik jich je
NS=r'http://www.volby.cz/prezident/'
pocet_res = repeat_get(URL)
pocet_sio = StringIO(pocet_res.content.decode())
pocet_xml = ET.parse(pocet_sio).getroot()
pocet = pocet_xml.find(r'./DAVKA', namespaces={'': NS}).attrib[r'PORADI_DAVKY']
pocet = int(pocet)
davky = Path('./davky')
davky.mkdir(exist_ok=True)
assert davky.is_dir()
print(f'Stahuji {pocet} dávek…')
for davka in range(1, pocet+1):
url = URL + str(davka)
resp = repeat_get(url)
FN = r'./davky/davka_{n:03}.xml'
with open(FN.format(n=davka), 'wb') as f:
f.write(resp.content)
print(f'Dávka {davka} stažena.')