-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindia2.py
65 lines (51 loc) · 1.44 KB
/
india2.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
import requests
from bs4 import BeautifulSoup
import time
def getIndiaData():
indiaData = {
'success': True,
'state': []
}
try:
htmlBody = requests.get('https://www.mygov.in/covid-19')
except requests.exceptions.RequestException as e:
indiaData['success'] = False
indiaData['error'] = str(e)
return indiaData
soup = BeautifulSoup(htmlBody.text, 'lxml')
table = soup.table
table_rows = table.find_all('tr')[1:]
for tr in table_rows:
td = tr.find_all('td')
row = [i.text for i in td]
state_name = row[0]
print(state_name)
try:
confirmed = row[1]
except AttributeError:
confirmed = None
print(confirmed)
try:
active = row[2]
except AttributeError:
active = None
print(active)
try:
recovered = row[3]
except AttributeError:
recovered = None
print(recovered)
try:
deaths = row[4]
except AttributeError:
deaths = None
print(deaths)
indianData = {
'state_name': state_name,
'confirmed': confirmed,
'active': active,
'recovered': recovered,
'deaths': deaths
}
indiaData['state'].append(indianData)
return indiaData