Skip to content

Instantly share code, notes, and snippets.

@karanlyons
Created February 6, 2020 03:45
Show Gist options
  • Select an option

  • Save karanlyons/c3b29f577de96082f6b6c6fcec578232 to your computer and use it in GitHub Desktop.

Select an option

Save karanlyons/c3b29f577de96082f6b6c6fcec578232 to your computer and use it in GitHub Desktop.

Revisions

  1. karanlyons created this gist Feb 6, 2020.
    1 change: 1 addition & 0 deletions IA.json
    1 addition, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    2 changes: 2 additions & 0 deletions meta.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    Results as of 2020-02-06T03:44:21+0000
    92% reporting (1624 of 1765 precincts)
    43 changes: 43 additions & 0 deletions parse.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-

    from decimal import Decimal
    from itertools import zip_longest, chain, repeat

    import httpx
    import ujson as json
    from bs4 import BeautifulSoup as bs


    # Don't you dare judge me.


    iowa_req = httpx.get('https://results.thecaucuses.org')
    results = bs(iowa_req.text, 'lxml').find('section', {'class', 'precinct-results'})
    candidates = [el.text for el in results.find('ul', {'class': 'thead'}).find_all('li')[2::3]]
    parts = [el.text for el in results.find('ul', {'class': 'sub-head'}).find_all('li')[2:]]


    keys = list(zip(chain.from_iterable(repeat(c, 3) for c in candidates), parts))

    def unpack(candidates, precinct):
    dct = {c: {} for c in candidates}

    for candidate, part, value in [
    list(key) + [Decimal(res.text.replace(',', ''))]
    for key, res in zip(keys, precinct.find_all('li')[1:])
    ]:
    dct[candidate][part] = value

    return dct


    counties = {
    county.find('div', {'class': 'precinct-county'}).text: {
    precinct.find('li').text: unpack(candidates, precinct)
    for precinct in county.find('div', {'class': 'precinct-data'}).find_all('ul')
    }
    for county in results.find_all('div', {'class': 'precinct-rows'})
    }

    print(json.dumps(counties))