Skip to content

Instantly share code, notes, and snippets.

@ivirshup
Created March 5, 2020 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivirshup/f9fcb18513e660396a7426660db0ee1f to your computer and use it in GitHub Desktop.
Save ivirshup/f9fcb18513e660396a7426660db0ee1f to your computer and use it in GitHub Desktop.
Get delegate counts from nyt, since their visualizations suck
"""
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