Last active 1442626767

Scrape and sort results for Canning by-election. Requirements: python 2.7 probably, requests, beautifulsoup4, tabulate

Revision a406b6128898d1cd6f059146f89bf7335810bf12

aec-canning-2015.py Raw
1import requests
2from bs4 import BeautifulSoup as Soup
3from tabulate import tabulate
4import sys
5
6headers = ["Candidate", "Party", "Votes", "%", "Swing (%)"]
7
8def get_data():
9 soup = Soup(requests.get("http://vtr.aec.gov.au/HouseDivisionFirstPrefs-18126-236.htm").text, "html.parser")
10 for row in soup.findAll('tr', id=lambda x: x and x.startswith("repeaterCandidates")):
11 yield [td.text for td in row.findAll('td')]
12
13def main():
14 if len(sys.argv) > 1:
15 if not sys.argv[1].isdigit():
16 print "Usage: {} <column number to sort on>".format(sys.argv[0])
17 return 1
18 col = int(sys.argv[1])
19 else:
20 col = 3
21 print tabulate(sorted(data, key=lambda x: float(x[col]), reverse=True), headers=headers)
22 return 0
23
24if __name__ == "__main__":
25 sys.exit(main())