-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (44 loc) · 1.63 KB
/
app.py
File metadata and controls
51 lines (44 loc) · 1.63 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
import csv
import requests
import unicodedata
import re
import datetime
from git import Repo
url = 'http://santemontreal.qc.ca/population/coronavirus-covid-19/situation-du-coronavirus-covid-19-a-montreal/'
ua = UserAgent()
header = {'User-Agent': str(ua.chrome)}
response = requests.get(url, headers=header)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.findAll('table', attrs={'class': 'contenttable'})[3]
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)
date = yesterday.strftime('%Y-%m-%d')
output_rows = [['Arrondissements', 'Cas Confirmés']]
for table_row in table.findAll('tr'):
cities = table_row.findAll('td')[0:1]
cases = table_row.findAll('td')[4:5]
output_row = []
for i in range(len(cities)):
city = cities[i].text
city = unicodedata.normalize('NFKD', city).strip()
city = city.replace('*', '')
if re.search('[a-zA-Z]', city) is None:
city = city.replace(' ', '')
elif city == 'Territoire à confirmer3':
city = city.replace('3', '')
case_total = cases[i].text
case_total = case_total.replace(' ', '')
output_row.append(city)
output_row.append(case_total)
output_rows.append(output_row)
output_rows = filter(None, output_rows)
with open(f'data/{str(date)}.csv', 'w', encoding='utf-8', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(output_rows)
repo = Repo('.git')
repo.git.add(all=True)
repo.index.commit(f'Added data for {str(date)}')
origin = repo.remote(name='origin')
origin.push()