Skip to content

Instantly share code, notes, and snippets.

@Kuanlin-Chen
Last active January 8, 2019 12:15
Show Gist options
  • Save Kuanlin-Chen/d27089b611cce8fc363f1758acbcb557 to your computer and use it in GitHub Desktop.
Save Kuanlin-Chen/d27089b611cce8fc363f1758acbcb557 to your computer and use it in GitHub Desktop.
Scrap and print data from 2018 Referendum.
from lxml import html
from lxml import etree
import requests
taipei_district_dic = {1:[527,644], 2:[644,786], 3:[1214,1402], 4:[786,919], 5:[1001,1097], 6:[919,1001],
7:[1097,1214], 8:[1402,1564], 9:[455,527], 10:[305,455], 11:[142,305], 12:[1,142]}
print '==================== Vote for Same-Sex Marriage ===================='
for num in range(1,13):
url_head = 'http://referendum.2018.nat.gov.tw/pc/zh_TW/08/m6300000'
url_center = '0000'
url_end = '.html'
for n in range(taipei_district_dic[num][0],taipei_district_dic[num][1]):
page = requests.get(url_head+str("%02d"%num)+url_center+str("%04d"%n)+url_end)
tree = html.fromstring(page.content)
area = tree.xpath('//td[@valign="bottom"]//text()')
vote = tree.xpath('//tr[@class="trT"]/td/text()')
# Convert 1,561 into 1561
agree = int(vote[0].replace(',',''))
disagree = int(vote[1].replace(',',''))
if agree > disagree:
percentage = '{:.0%}'.format(float(agree)/float((agree+disagree)))
print area[0] + ' NO.' + str(n) + ' Argee:' + str(agree) + ' Disagree:' + str(disagree) + ' ' + percentage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment