Last active 1442626767

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

Steven Smith revised this gist 1442662767. Go to revision

No changes

Steven Smith revised this gist 1442662718. Go to revision

1 file changed, 1 insertion

aec-canning-2015.py

@@ -18,6 +18,7 @@ def main():
18 18 col = int(sys.argv[1])
19 19 else:
20 20 col = 3
21 + data = [d for d in get_data()]
21 22 print tabulate(sorted(data, key=lambda x: float(x[col]), reverse=True), headers=headers)
22 23 return 0
23 24

Steven Smith revised this gist 1442662698. Go to revision

1 file changed, 1 insertion, 1 deletion

aec-canning-2015.py

@@ -1,7 +1,7 @@
1 1 import requests
2 2 from bs4 import BeautifulSoup as Soup
3 3 from tabulate import tabulate
4 - from sys import argv
4 + import sys
5 5
6 6 headers = ["Candidate", "Party", "Votes", "%", "Swing (%)"]
7 7

Steven Smith revised this gist 1442662643. Go to revision

1 file changed, 25 insertions

aec-canning-2015.py(file created)

@@ -0,0 +1,25 @@
1 + import requests
2 + from bs4 import BeautifulSoup as Soup
3 + from tabulate import tabulate
4 + from sys import argv
5 +
6 + headers = ["Candidate", "Party", "Votes", "%", "Swing (%)"]
7 +
8 + def 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 +
13 + def 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 +
24 + if __name__ == "__main__":
25 + sys.exit(main())
Newer Older