-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcovidScrapen.py
90 lines (64 loc) · 2.04 KB
/
covidScrapen.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import requests
from lxml import html
import pandas as pd
import numpy as np
from pymaran import gSheets,mail
import datetime
# In[18]:
def scrap2sheet(url,
xPath='//*[@id="main"]/div[1]/table/*',
tableElement=2,
autoDate=True,
manualDate='',
n_cols = 5):
global tree
### https://www.rki.de/DE/Content/InfAZ/N/Neuartiges_Coronavirus/Fallzahlen.html
pageContent=requests.get(url)
tree = html.fromstring(pageContent.content)
table = tree.xpath(xPath)
tbody = table[tableElement]
df_col = range(n_cols)
#df_col = ['city','total_cases','daily_cases','cases_per_M','total_deaths','']
df = pd.DataFrame(columns=df_col)
count = 0
for tr in tbody:
temp_data = []
for td in tr:
#print(td.text_content())
temp_data.append(td.text_content())
df.loc[count] = temp_data
count += 1
if autoDate:
df.loc[:,'date'] = datetime.date.today()
df['date'] = df['date'].astype(str)
else:
df['date'] = manualDate
sheet = gSheets.readSheet('covidCrawlen')
### Save parsed data
df[1] = pd.to_numeric(df[1])
df[4] = pd.to_numeric(df[4])
df.loc[df[1].apply(lambda x: ~ (float(x) == np.floor(x))),1] = df[1]*1000
df.columns = ['city','total_cases','new_cases','deaths_per_M','total_deaths','date']
df = df.reindex(['city','total_cases','total_deaths','date'],axis=1)
sheet = gSheets.readSheet('covidCrawlen')
worksheet = sheet.worksheet('data')
for i,r in df.iterrows():
worksheet.append_row(list(r))
print('success' + str(datetime.date.today()))
return df
# In[19]:
try:
df = scrap2sheet(
'https://www.rki.de/DE/Content/InfAZ/N/Neuartiges_Coronavirus/Fallzahlen.html',
'//*[@id="main"]/div[1]/table/*',
2,
True,
'',
5)
except:
print('error' + str(datetime.date.today()))
# In[20]:
# In[ ]: