Skip to content

Instantly share code, notes, and snippets.

@starenka
Last active October 6, 2018 18:04
Show Gist options
  • Save starenka/eb66f5946cf243c9156477f3ef5503ec to your computer and use it in GitHub Desktop.
Save starenka/eb66f5946cf243c9156477f3ef5503ec to your computer and use it in GitHub Desktop.
# pip install requests xmltodict pandas
import datetime
import time
import os
import requests
import xmltodict
import pandas as pd
PRAHA = 554782
P7 = 500186
def parse(okr):
data = xmltodict.parse(requests.get(
'https://www.volby.cz/pls/kv2018/vysledky_obec?cislo_obce=%d' % okr, timeout=10).content)
res = data['VYSLEDKY_OBEC']['OBEC']['VYSLEDEK']
ucast = res['UCAST']
parties = res['VOLEBNI_STRANA']
parsed = {p['@NAZEV_STRANY']: (p['@ZASTUPITELE_POCET'], p['@HLASY_PROC'],
p['@HLASY'], p['@ZASTUPITELE_PROC']) for p in parties}
df = pd.DataFrame(parsed).T
df.columns = ('zas_mandatu', 'proc', 'hlasu', 'zas_proc')
df['proc'] = pd.to_numeric(df['proc'])
df['hlasu'] = pd.to_numeric(df['hlasu'])
df['zas_proc'] = pd.to_numeric(df['zas_proc'])
df['zas_mandatu'] = pd.to_numeric(df['zas_mandatu'])
sor = df.sort_values(['zas_mandatu', 'proc'], axis=0, ascending=False)
return sor, ucast
def show(okr, label):
data, ucast = parse(okr)
print '\n===== %s ===== \t\tzpracovano: %s%%\t ucast: %s%%' % (label, ucast['@OKRSKY_ZPRAC_PROC'], ucast['@UCAST_PROC'])
print data.head(10)
while True:
os.system('clear')
print 'data: %s' % datetime.datetime.now()
show(PRAHA, 'MAGISTRAT')
show(P7, 'PRAHA 7')
time.sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment