Created
March 5, 2020 02:53
-
-
Save ivirshup/f9fcb18513e660396a7426660db0ee1f to your computer and use it in GitHub Desktop.
Get delegate counts from nyt, since their visualizations suck
This file contains 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 characters
""" | |
TODO: | |
* Get dates for each state | |
""" | |
from requests_html import HTMLSession | |
import pandas as pd | |
import string | |
URL = "https://www.nytimes.com/interactive/2020/us/elections/delegate-count-primary-results.html" | |
def get_delegates(table): | |
del_counts = {} | |
del_counts["state"] = [x.text for x in table.find(".g-full-name")] | |
for k in ["biden", "sanders", "warren", "bloomberg"]: | |
del_counts[k] = [x.text for x in table.find(f".g-{k}")] | |
dels = pd.DataFrame(del_counts) | |
dels.set_index("state", inplace=True) | |
dels[~dels.applymap(lambda x: all(y in string.digits for y in x))] = 0 | |
dels = dels.astype(int) | |
return dels | |
def get_delegates_table(url=URL): | |
return ( | |
HTMLSession() | |
.get(url) | |
.html | |
.find("#g-delegate-table") | |
)[0] | |
table = get_delegates_table() | |
dels = get_delegates(table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment