-
Notifications
You must be signed in to change notification settings - Fork 0
/
countires.py
66 lines (52 loc) · 1.58 KB
/
countires.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
import requests
from bs4 import BeautifulSoup
import time
def getData():
countryData = {
'success': True,
'data': []
}
try:
htmlBody = requests.get(
'https://www.worldometers.info/coronavirus/#countries')
except requests.exceptions.RequestException as e:
countryData['success'] = False
countryData['error'] = str(e)
return countryData
soup = BeautifulSoup(htmlBody.text, 'lxml')
table = soup.table
table_rows = table.find_all('tr')[9:-8]
for tr in table_rows:
td = tr.find_all('td')[1:]
row = [i.text for i in td]
country = row[0]
try:
total_cases = row[1]
except AttributeError:
total_cases = None
try:
active_cases = row[6]
except AttributeError:
active_cases = None
try:
total_deaths = row[3]
except AttributeError:
total_deaths = None
try:
total_recovered = row[5]
except AttributeError:
total_recovered = None
try:
new_cases = row[2]
except AttributeError:
new_cases = None
countriesData = {
'country': country,
'total_cases': total_cases,
'active_cases': active_cases,
'total_deaths': total_deaths,
'total_recovered': total_recovered,
'new_cases': new_cases
}
countryData['data'].append(countriesData)
return countryData