Skip to content

Instantly share code, notes, and snippets.

@blha303
Last active September 19, 2015 11:39
Show Gist options
  • Save blha303/a312976c48c84e60b846 to your computer and use it in GitHub Desktop.
Save blha303/a312976c48c84e60b846 to your computer and use it in GitHub Desktop.
Scrape and sort results for Canning by-election. Requirements: python 2.7 probably, requests, beautifulsoup4, tabulate
import requests
from bs4 import BeautifulSoup as Soup
from tabulate import tabulate
import sys
headers = ["Candidate", "Party", "Votes", "%", "Swing (%)"]
def get_data():
soup = Soup(requests.get("http://vtr.aec.gov.au/HouseDivisionFirstPrefs-18126-236.htm").text, "html.parser")
for row in soup.findAll('tr', id=lambda x: x and x.startswith("repeaterCandidates")):
yield [td.text for td in row.findAll('td')]
def main():
if len(sys.argv) > 1:
if not sys.argv[1].isdigit():
print "Usage: {} <column number to sort on>".format(sys.argv[0])
return 1
col = int(sys.argv[1])
else:
col = 3
data = [d for d in get_data()]
print tabulate(sorted(data, key=lambda x: float(x[col]), reverse=True), headers=headers)
return 0
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment