Python – Get Confirmed, Recovered, Deaths cases of Corona around the globe
In this article we will see how we can create a python script which tells about the cases of corona around the world i.e number of confirmed cases, number of cases in which patient has recovered and total deaths due to corona.
Modules required and Installation:
Requests : Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs.
pip install requests
Beautiful Soup: Beautiful Soup is a library that makes it easy to scrape information from web pages. It sits atop an HTML or XML parser, providing Pythonic idioms for iterating, searching, and modifying the parse tree.
pip install beautifulsoup4
Explanation –
We will scrap the data from the website with the help of requests
BeautifulSoup
then we will convert the raw data into html code and then filter it to get the required output and will save the info in the dictionary.
Below is the implementation –
Python3
from bs4 import BeautifulSoup as BS
import requests
def get_info(url):
data = requests.get(url)
soup = BS(data.text, 'html.parser' )
total = soup.find( "div" , class_ = "maincounter-number" ).text
total = total[ 1 : len (total) - 2 ]
other = soup.find_all( "span" , class_ = "number-table" )
recovered = other[ 2 ].text
deaths = other[ 3 ].text
deaths = deaths[ 1 :]
ans = { 'Total Cases' : total, 'Recovered Cases' : recovered,
'Total Deaths' : deaths}
return ans
ans = get_info(url)
for i, j in ans.items():
print (i + " : " + j)
|
Output :
Total Cases : 2,129,927
Recovered Cases : 539,063
Total Deaths : 142,716
Last Updated :
10 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...