-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikiscrape.py
45 lines (33 loc) · 1.04 KB
/
wikiscrape.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
from datetime import datetime as dt
import requests
import bs4
def get_date_str():
ds = str(dt.now().date())
return ds.replace('-', '.')
def get_wikipage():
res = None
try:
res = requests.get('https://en.wikipedia.org/wiki/COVID-19_pandemic_by_country_and_territory')
res.raise_for_status()
except Exception as err:
print(err)
return res
def get_world_count(table):
th = table.select('th')
world = str(th[8])
return world[-15:-6].replace(',', '')
def get_US_count(table):
td = table.select('td')
US = str(td[1])
return US[-13:-6].replace(',', '')
if __name__ == '__main__':
d8 = get_date_str()
page = get_wikipage()
if page is not None:
soup = bs4.BeautifulSoup(page.text, 'html.parser')
table = soup.find('table', {'id':'thetable'})
world = get_world_count(table)
US = get_US_count(table)
print('{0}, {1}, {2}'.format(d8, US, world))
else:
print('error: no internet connection, perhaps?')