Created
February 6, 2020 03:45
-
-
Save karanlyons/c3b29f577de96082f6b6c6fcec578232 to your computer and use it in GitHub Desktop.
Revisions
-
karanlyons created this gist
Feb 6, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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))