Last active
April 22, 2022 00:33
-
-
Save mrcreel/5432787a57b131a87c3f1724b1f7fffd to your computer and use it in GitHub Desktop.
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
import csv # Needed to write the output file | |
import requests # Needed to call the website | |
from bs4 import BeautifulSoup # The miracle worker that reads the page data | |
URL = 'https://msdh.ms.gov/msdhsite/_static/14,0,420.html' # Your site | |
page = requests.get(URL) # Imports the page | |
soup = BeautifulSoup(page.content, 'html.parser') # Converts the page's html into text | |
cases = soup.find(id='msdhTotalCovid-19Cases') # Find the table with that id | |
counties = cases.find_all('tr') # Looks inside the table and makes an array of all the <tr>, or row, elements as counties | |
counties_totals = [] # Create an empty list to store the totals data | |
for county in counties: | |
data = county.find_all('td') # For each county, make an array of each <td>, or data, element | |
county_name = data[0].text # Set county_name as the text insid th 0th data element | |
if (county_name != 'County' and county_name != 'Total'): # Gets rid of the first and last row | |
# For the three numeric cols, if they are blank set to 0 | |
county_cases = data[1].text | |
if (county_cases == ' '): | |
county_cases = 0 | |
county_deaths = data[2].text | |
if (county_deaths == ' '): | |
county_deaths = 0 | |
county_ltcs = data[3].text | |
if (county_ltcs == ' '): | |
county_ltcs = 0 | |
# Make a list for each county, making sure the numbers are actually numbers with int() | |
county_data = [county_name, int(county_cases), int( | |
county_deaths), int(county_ltcs)] | |
# Add the county data to the main list | |
counties_totals.append(county_data) | |
# Extremely janky code to test if issaquena if it's still at 0 cases | |
# and if so, add it | |
# Set default that issaquena is still not there | |
issaquena_test = False | |
# Loop through each county to see if it's Issaquena | |
for row in counties_totals: | |
# if issaquena exists, set to true | |
if (row[0] == 'Issaquena'): | |
issaquena_test = True | |
# if it still has no cases add it and sort list | |
if(issaquena_test == False): | |
counties_totals.append(['Issaquena', 0, 0, 0]) | |
counties_totals.sort(key=lambda x: x[0]) | |
print(counties_totals) | |
# Write to csv | |
with open("out.csv", "w", newline="") as f: | |
writer = csv.writer(f) | |
writer.writerows(counties_totals) |
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
Adams | 73 | 5 | 2 | |
---|---|---|---|---|
Alcorn | 7 | 0 | 0 | |
Amite | 17 | 1 | 0 | |
Attala | 31 | 0 | 1 | |
Benton | 6 | 0 | 0 | |
Bolivar | 76 | 5 | 4 | |
Calhoun | 32 | 2 | 1 | |
Carroll | 10 | 1 | 0 | |
Chickasaw | 39 | 2 | 1 | |
Choctaw | 11 | 1 | 0 | |
Claiborne | 8 | 0 | 0 | |
Clarke | 21 | 1 | 1 | |
Clay | 22 | 1 | 0 | |
Coahoma | 45 | 1 | 0 | |
Copiah | 40 | 1 | 0 | |
Covington | 22 | 0 | 0 | |
Desoto | 213 | 3 | 1 | |
Forrest | 137 | 2 | 2 | |
Franklin | 14 | 0 | 0 | |
George | 9 | 0 | 0 | |
Greene | 3 | 0 | 0 | |
Grenada | 15 | 0 | 1 | |
Hancock | 50 | 5 | 2 | |
Harrison | 137 | 5 | 2 | |
Hinds | 314 | 5 | 5 | |
Holmes | 54 | 4 | 0 | |
Humphreys | 12 | 3 | 1 | |
Issaquena | 0 | 0 | 0 | |
Itawamba | 13 | 1 | 1 | |
Jackson | 196 | 6 | 2 | |
Jasper | 20 | 1 | 0 | |
Jefferson | 4 | 0 | 1 | |
Jefferson Davis | 8 | 1 | 0 | |
Jones | 56 | 0 | 3 | |
Kemper | 16 | 0 | 0 | |
Lafayette | 39 | 2 | 1 | |
Lamar | 57 | 1 | 0 | |
Lauderdale | 181 | 13 | 6 | |
Lawrence | 12 | 0 | 0 | |
Leake | 60 | 1 | 0 | |
Lee | 49 | 4 | 0 | |
Leflore | 70 | 6 | 1 | |
Lincoln | 95 | 6 | 2 | |
Lowndes | 30 | 1 | 1 | |
Madison | 123 | 3 | 3 | |
Marion | 38 | 0 | 1 | |
Marshall | 37 | 2 | 0 | |
Monroe | 58 | 3 | 2 | |
Montgomery | 15 | 1 | 0 | |
Neshoba | 46 | 1 | 0 | |
Newton | 19 | 0 | 1 | |
Noxubee | 20 | 0 | 0 | |
Oktibbeha | 42 | 2 | 2 | |
Panola | 27 | 2 | 0 | |
Pearl River | 115 | 8 | 2 | |
Perry | 21 | 1 | 0 | |
Pike | 93 | 2 | 2 | |
Pontotoc | 17 | 2 | 1 | |
Prentiss | 16 | 0 | 2 | |
Quitman | 13 | 0 | 0 | |
Rankin | 128 | 2 | 1 | |
Scott | 96 | 0 | 1 | |
Sharkey | 3 | 0 | 0 | |
Simpson | 14 | 0 | 1 | |
Smith | 38 | 1 | 1 | |
Stone | 17 | 0 | 0 | |
Sunflower | 45 | 2 | 0 | |
Tallahatchie | 8 | 1 | 0 | |
Tate | 27 | 0 | 0 | |
Tippah | 45 | 6 | 1 | |
Tishomingo | 2 | 0 | 0 | |
Tunica | 28 | 1 | 1 | |
Union | 9 | 1 | 1 | |
Walthall | 22 | 0 | 0 | |
Warren | 17 | 1 | 0 | |
Washington | 68 | 2 | 1 | |
Wayne | 10 | 0 | 0 | |
Webster | 17 | 1 | 0 | |
Wilkinson | 53 | 3 | 1 | |
Winston | 27 | 0 | 0 | |
Yalobusha | 14 | 0 | 0 | |
Yazoo | 81 | 1 | 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment