-
Notifications
You must be signed in to change notification settings - Fork 0
/
convidscraper.py
39 lines (33 loc) · 1.12 KB
/
convidscraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python
#pip install requests
#pip install bs4
from datetime import datetime, date
import os.path
from os import path
import requests
from bs4 import BeautifulSoup
import csv
dt = datetime.now().strftime("%Y-%m-%d")
URL = "https://dph.georgia.gov/covid-19-daily-status-report"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html.parser')
data=[]
for caption in soup.find_all('caption'):
if caption.get_text() == 'COVID-19 Confirmed Cases by County':
table = caption.find_parent('table', {'class': 'stacked-row-plus'})
table_body = table.find('tbody')
rows = table_body.find_all('tr')
for row in rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele]) # Get rid of empty values
filename = 'georgia_convid19.csv'
noheader=False
if os.path.exists(filename):
noheader=True
with open(filename, 'ab') as f:
w = csv.DictWriter(f,['date','county','count'])
if noheader != True:
w.writeheader()
for d in data:
w.writerow({'date': dt, 'county' : d[0], 'count' : d[1]})