Skip to content

Instantly share code, notes, and snippets.

@danielsuo
Created November 6, 2020 16:20
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 danielsuo/79dc104ae979b61e6bc7a32adc871ba6 to your computer and use it in GitHub Desktop.
Save danielsuo/79dc104ae979b61e6bc7a32adc871ba6 to your computer and use it in GitHub Desktop.
import bs4
import requests
import pandas as pd
def scrape(state):
soup = bs4.BeautifulSoup(requests.get(f"https://www.foxnews.com/elections/2020/general-results/state/{state}").content.decode("utf-8"))
section = soup.find_all("section", class_="is-president")[0]
section = section.find_all("div", class_="race-table")[0]
tables = section.find_all("table")
results = []
for table in tables:
county = table.find_all("span", class_="county")[0].decode_contents().strip()
candidates = table.find_all("tr", class_="candidate")
result = {"state": state, "county": county}
for candidate in candidates:
name = candidate.find_all("span", class_="is-short")[0].decode_contents().strip()
count = int(candidate.find_all("span", class_="count")[0].decode_contents().strip().replace(",", ""))
result[name] = count
result["percent"] = float(table.find_all("span", class_="percent-in")[0].decode_contents().strip().replace("Reporting ", "").replace("% in", ""))
results.append(result)
return pd.DataFrame(results)
def margin(df):
votes = df.drop(labels=["state", "county", "percent"], axis=1)
df["current"] = votes.sum(axis=1)
df["final"] = df.current / df.percent * 100
for candidate in votes.columns:
df[candidate] *= (df.final / df.current)
return df.drop(labels=["state", "county", "percent", "current", "final"], axis=1).sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment