Last active
April 20, 2020 13:51
-
-
Save gamesbook/c907a040f9eae0843ae79b36b9f30687 to your computer and use it in GitHub Desktop.
Python script to access Corona / COVID-19 data from worldometers as Excel or plain text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
"""Command-line access to stats from https://www.worldometers.info/coronavirus/ | |
Created: 2020-04-05 | |
Author: Derek <gamesbook@gmail.com> | |
Requires: | |
chromedriver - https://chromedriver.chromium.org/ | |
xlsxwriter - https://xlsxwriter.readthedocs.io/ | |
Source: | |
https://github.com/lazargugleta/coronavirusStats/blob/master/coronavirus.py | |
""" | |
# lib | |
import argparse | |
from collections import namedtuple | |
from datetime import datetime | |
import re | |
from time import sleep | |
# other | |
from selenium import webdriver | |
import xlsxwriter | |
fields = ( | |
'country', 'total_cases', 'new_cases', 'total_deaths', 'new_deaths', | |
'active_cases', 'total_recovered', 'serious_critical') | |
WorldMeter = namedtuple('WorldMeter', fields, defaults=(None,) * len(fields)) | |
class Coronavirus(): | |
def __init__(self): | |
self.driver = webdriver.Chrome() | |
def get_all(self): | |
records = [] | |
self.driver.get('https://www.worldometers.info/coronavirus/') | |
table = self.driver.find_element_by_xpath( | |
'//*[@id="main_table_countries_today"]/tbody[1]') | |
rows = table.find_elements_by_tag_name("tr") | |
for row in rows: | |
data = row.find_elements_by_tag_name("td") | |
record = WorldMeter( | |
country=data[0].text, | |
total_cases=self.clean(data[1].text), | |
new_cases=self.clean(data[2].text), | |
total_deaths=self.clean(data[3].text), | |
new_deaths=self.clean(data[4].text), | |
total_recovered=self.clean(data[5].text), | |
active_cases=self.clean(data[6].text), | |
serious_critical=self.clean(data[7].text), | |
) | |
records.append(record) | |
self.driver.close() | |
self.driver.quit() | |
return records | |
def get_excel(self): | |
records = self.get_all() | |
#print(records) | |
headers = [ | |
'Country', | |
'Cases, Total', | |
'Cases, New', | |
'Deaths, Total', | |
'Deaths, New', | |
'Total Recovered', | |
'Active Cases', | |
'Serious, Critical'] | |
workbook = xlsxwriter.Workbook('covid19.xlsx') | |
worksheet = workbook.add_worksheet() | |
for col, header in enumerate(headers): | |
worksheet.write(0, col, header) | |
row, col = 1, 0 | |
for item in records: | |
worksheet.write(row, col, item.country) | |
worksheet.write(row, col + 1, item.total_cases) | |
worksheet.write(row, col + 2, item.new_cases) | |
worksheet.write(row, col + 3, item.total_deaths) | |
worksheet.write(row, col + 4, item.new_deaths) | |
worksheet.write(row, col + 5, item.total_recovered) | |
worksheet.write(row, col + 6, item.active_cases) | |
worksheet.write(row, col + 7, item.serious_critical) | |
row += 1 | |
workbook.close() | |
def get_data(self, country="USA"): | |
self.driver.get('https://www.worldometers.info/coronavirus/') | |
table = self.driver.find_element_by_xpath( | |
'//*[@id="main_table_countries_today"]/tbody[1]') | |
country_element = table.find_element_by_xpath( | |
"//td[contains(., '%s')]" % country) | |
row = country_element.find_element_by_xpath("./..") | |
data = row.find_elements_by_tag_name("td") | |
record = WorldMeter( | |
country=country_element.text, | |
total_cases=self.clean(data[1].text), | |
new_cases=self.clean(data[2].text), | |
total_deaths=self.clean(data[3].text), | |
new_deaths=self.clean(data[4].text), | |
total_recovered=self.clean(data[5].text), | |
active_cases=self.clean(data[6].text), | |
serious_critical=self.clean(data[7].text), | |
) | |
self.driver.close() | |
self.driver.quit() | |
return record | |
def print_data(self, record): | |
print("Country: {}".format(record.country)) | |
print("Total cases: {:,}".format(record.total_cases)) | |
print("New cases: +{}".format(record.new_cases)) | |
print("Total deaths: {:,}".format(record.total_deaths)) | |
print("New deaths: +{}".format(record.new_deaths)) | |
print("Total recovered: {:,}".format(record.total_recovered)) | |
print("Active cases: {:,}".format(record.active_cases)) | |
print("Serious, critical cases: {:,}".format(record.serious_critical)) | |
def clean(self, data): | |
try: | |
return int(data.replace(",", "").replace("+", "")) | |
except: | |
return 0 | |
def main(args): | |
"""Process args and run.""" | |
covid = Coronavirus() | |
if args.excel: | |
covid.get_excel() | |
else: | |
record = covid.get_data(country=args.country) | |
covid.print_data(record) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'-c', '--country', default="USA", | |
help="Name of the country",) | |
parser.add_argument( | |
'-x', '--excel', action='store_true', | |
help="Download excel",) | |
ARGS = parser.parse_args() | |
main(ARGS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment