-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathliberia.py
63 lines (46 loc) · 1.32 KB
/
liberia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Liberia election results
import sys
sys.path.append('/usr/lib/python2.7/dist-packages/')
from bs4 import BeautifulSoup
import urllib2
import csv
import os
import re
os.chdir("/home/andrew/Dropbox/python/presentation/")
######## Pres/VP #####################
f= csv.writer(open("presidential_res.csv", "w"))
f.writerow(["PresVPcand","lastname","party","votes","county","precinct"]) # Write column headers as the first line
#orig
page1 = urllib2.urlopen("http://www.necliberia.org/results2011/pp_results/03002.html")
page2 = urllib2.urlopen("http://www.necliberia.org/results2011/pp_results/09006.html")
pages = [page1, page2]
for page in pages:
soup = BeautifulSoup(page)
# h2 - has county
h2 = soup.find_all("h2")
county = str(h2[0].get_text())
#h4 has voting precinct.
h4 = soup.find_all("h4")
precinct = str(h4[0].get_text())
# get the results tables
res = soup.findAll('div', {'class': 'res'})
pres = res[0]
for row in pres.find_all("tr"):
tds = row.find_all("td")
try:
a = str(tds[0].get_text())
b = str(tds[1].get_text())
except:
print "bad string"
continue
m = re.search(r'\(([A-Z]+)\)', a)
if m:
party = m.group(1)
else:
party = ''
m = re.search(r'([A-Z]+),', a)
if m:
lastname = m.group(1)
else:
lastname = ''
f.writerow([a, lastname, party, b, county, precinct])