Coronavirus, one of the biggest pandemic has brought all the world to Danger. Along with this, it is one of the trending News, everyone has this day. In this article, we will be scraping data and printing Covid-19 statistics in human-readable form. The data will be scraped from this website
Prerequisites:
- The libraries ‘requests’, ‘bs4’, and ‘texttable’ have to be installed –
pip install bs4
pip install requests
pip install texttable
Project : Let’s head over to code, create a file called run.py.
Python3
import requests
from bs4 import BeautifulSoup
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser' )
data = []
data_iterator = iter (soup.find_all( 'td' ))
while True :
try :
country = next (data_iterator).text
confirmed = next (data_iterator).text
deaths = next (data_iterator).text
continent = next (data_iterator).text
data.append((
country,
int (confirmed.replace( ', ' , '')),
int (deaths.replace( ', ' , '')),
continent
))
except StopIteration:
break
data.sort(key = lambda row: row[ 1 ], reverse = True )
|
To print the data in human-readable format, we will use the library ‘texttable‘
Python3
import texttable as tt
table = tt.Texttable()
table.add_rows([( None , None , None , None )] + data)
table.set_cols_align(( 'c' , 'c' , 'c' , 'c' ))
table.header(( ' Country ' , ' Number of cases ' , ' Deaths ' , ' Continent ' ))
print (table.draw())
|
Output:
+---------------------------+-------------------+----------+-------------------+
| Country | Number of cases | Deaths | Continent |
+===========================+===================+==========+===================+
| United States | 644348 | 28554 | North America |
+---------------------------+-------------------+----------+-------------------+
| Spain | 180659 | 18812 | Europe |
+---------------------------+-------------------+----------+-------------------+
| Italy | 165155 | 21645 | Europe |
+---------------------------+-------------------+----------+-------------------+
...
NOTE: The output depends on the current statistics
Stay home, stay safe!