Last active
September 28, 2017 08:48
-
-
Save gokceneraslan/9905246 to your computer and use it in GitHub Desktop.
yenimahalle-cankaya-kecioren fetcher/parser/nicer/dicer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib2 as ul | |
from lxml import etree | |
def parse_votes(url): | |
doc = ul.urlopen(url).read() | |
tree = etree.HTML(doc) | |
res = {} | |
try: | |
res['akp'] = int(tree.xpath('//*[@id="TabContainer_TabPanel1_rptPartiler_1_1_tbPartiOy_1_1_5"]')[0].attrib['value']) | |
except KeyError: | |
res['akp'] = 0 | |
try: | |
res['chp'] = int(tree.xpath('//*[@id="TabContainer_TabPanel1_rptPartiler_1_2_tbPartiOy_1_2_4"]')[0].attrib['value']) | |
except KeyError: | |
res['chp'] = 0 | |
ililcesandik = tree.xpath('//*[@id="TabContainer_TabPanel1_lblOzetIlIlce1"]')[0].text.split('/') | |
res['il'] = ililcesandik[0].strip().encode('utf-8') | |
res['ilce'] = ililcesandik[1].strip().encode('utf-8') | |
res['sandik'] = int(ililcesandik[2].strip()) | |
return res | |
def write_file(flname, sidrange, chpurltemplate): | |
with open(flname, 'w') as fl: | |
fl.write('il,ilce,sandik,chp,akp\n') | |
for sid in sidrange: | |
try: | |
res = parse_votes(chpurltemplate % {'sid': sid}) | |
print res | |
fl.write('%s,%s,%i,%i,%i\n' % (res['il'], res['ilce'], res['sandik'], res['chp'], res['akp'])) | |
except ul.HTTPError: | |
pass | |
cankayarange = range(206665, 208870) | |
chpurltemplate = 'http://sts.chp.org.tr/SonucDetay.aspx?sid=%(sid)s' | |
write_file('cankaya.csv', cankayarange, chpurltemplate) | |
yenimahallerange = range(216240, 217640) | |
write_file('yenimahalle.csv', yenimahallerange, chpurltemplate) | |
keciorenrange = range(210937, 212889) | |
write_file('kecioren.csv', keciorenrange, chpurltemplate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment