Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2012 12:32
Show Gist options
  • Save anonymous/4336370 to your computer and use it in GitHub Desktop.
Save anonymous/4336370 to your computer and use it in GitHub Desktop.
import re
import urllib
from bs4 import BeautifulSoup
import datetime
def read_percentage(soup):
return float(re.search(r'\((\d+\.\d+)\)',repr(soup)).groups()[0])
f = urllib.urlopen('http://info.nec.go.kr/electioninfo/electionInfo_report.xhtml?electionId=0020121219&requestURI=%2Felectioninfo%2F0020121219%2Fvc%2Fvccp09.jsp&topMenuId=VC&secondMenuId=VCCP&menuId=VCCP09&statementId=VCCP09_%231&electionCode=1&cityCode=0&sggCityCode=0&x=26&y=8')
html = f.read()
soup = BeautifulSoup(html)
trs = soup.find_all('tr')
park_sum = 0
moon_sum = 0
totals = [6309611,2219981,1586702,1657753,898481,904790,695145,64994,
7015827,912038,925829,1168205,1142216,1171230,1710245,2008921,330944]
i = 0
for tr in trs[3:]:
tds = tr.find_all('td')
total = float(re.sub(',','', tds[2].text))
park = read_percentage(tds[3])
moon = read_percentage(tds[4])
park_sum += totals[i]*park
moon_sum += totals[i]*moon
i += 1
print park_sum, moon_sum
print datetime.datetime.now()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment