Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
#!/usr/bin/env python
# coding=utf-8
import time
from pyquery import PyQuery as pq
def to_float(text):
return float(to_str(text).replace(',', '.').strip())
def to_str(el):
return el.text.encode('utf8')
def do(url, banner='', head=None):
doc = pq(url)
ns, vs, ps, cs = ('table td[headers="t2sa2 t2sb2"]',
'table td[headers="t2sa3 t2sb3"]',
'table td[headers="t2sa3 t2sb4"]',
'table td[headers="t2sa7"]',
names, percs, votes, chairs = doc.find(ns), doc.find(ps), doc.find(vs), doc.find(cs)
chairs = map(to_str, chairs) if chairs else ['n/a']*len(names)
ret = [(to_str(n), to_float(p), to_str(v), c) for n,p,v,c in zip(names, percs, votes, chairs)]
ret.sort(key=lambda one: one[1], reverse=True)
print '\n============= %s ==============' % banner.upper()
print '\nSECTENO: %s%%\n' % to_float(doc.find('td[headers="sa3 sb3"]')[0])
for name, perc, votes, chairs in ret if not head else ret[:head+1]:
print '%-50s%s%% (%s mandatu, %s hlasu)' % (name, perc, chairs, votes)
while True:
do('', 'p7', head=8)
do('', 'magistrat', head=8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.