Skip to content

Instantly share code, notes, and snippets.

@naiquevin
Created May 16, 2014 12:49
Show Gist options
  • Save naiquevin/9bc1056b47acdc66b3cd to your computer and use it in GitHub Desktop.
Save naiquevin/9bc1056b47acdc66b3cd to your computer and use it in GitHub Desktop.
elections2014
import os
import json
import requests
from lxml import html
def num_to_state(n):
assert n <= 35
if n < 10:
return 'S0{}'.format(n)
if n <= 28:
return 'S{}'.format(n)
return 'U0{}'.format(n-28)
def state_const_map():
e = html.parse('http://eciresults.nic.in/ConstituencywiseS011.htm?ac=1')
cts = e.xpath("//input[@type='hidden']/@value")
states = {s.attrib['value']: s.text for s in
e.xpath("//select[@name='ctl00$ContentPlaceHolder1$ACWise1$ddlState']/option")[1:]}
sc_map = ((num_to_state(i+1), dict([tuple(x.strip().strip(';').split(','))
for x in c.split('\r\n')]))
for i, c in enumerate(cts))
sc_map = {states[s]: {'code': s, 'constituencies': cs}
for s, cs in sc_map}
filepath = '/home/vineet/elections2014/state-const-map2.json'
with open(filepath, 'w') as f:
json.dump(sc_map, f, indent=4)
return sc_map
def ensure_dir(p):
if not os.path.exists(p):
os.makedirs(p)
return p
def constituency_wise():
dirpath = ensure_dir('/home/vineet/elections2014/constituency-wise')
state_const_map = json.load(open('/home/vineet/elections2014/state-const-map2.json'))
for k, v in state_const_map.iteritems():
scode = v['code']
for c, n in v['constituencies'].iteritems():
url = 'http://eciresults.nic.in/Constituencywise{}{}.htm?ac={}'.format(scode, c, c)
r = requests.get(url)
if r.status_code == 200:
p = ensure_dir(os.path.join(dirpath, k))
with open(os.path.join(p, '{}.html'.format(n)), 'w') as f:
f.write(r.content)
def write_html(filepath, content):
with open(filepath, 'w') as f:
f.write(content)
if __name__ == '__main__':
# state_const_map()
constituency_wise()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment