Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Get Confirmed, Recovered, Deaths cases of Corona around the globe

  • Last Updated : 17 Nov, 2021

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. 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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 – 


# importing libraries
from bs4 import BeautifulSoup as BS
import requests
# method to get the info
def get_info(url):
    # getting the request from url
    data = requests.get(url)
    # converting the text
    soup = BS(data.text, 'html.parser')
    # finding meta info for total cases
    total = soup.find("div", class_ = "maincounter-number").text
    # filtering it
    total = total[1 : len(total) - 2]
    # finding meta info for other numbers
    other = soup.find_all("span", class_ = "number-table")
    # getting recovered cases number
    recovered = other[2].text
    # getting death cases number
    deaths = other[3].text
    # filtering the data
    deaths = deaths[1:]
    # saving details in dictionary
    ans ={'Total Cases' : total, 'Recovered Cases' : recovered,
                                 'Total Deaths' : deaths}
    # returning the dictionary
    return ans
# url of the corona virus cases
# calling the get_info method
ans = get_info(url)
# printing the ans
for i, j in ans.items():
    print(i + " : " + j)

Output : 

Total Cases : 2,129,927
Recovered Cases : 539,063
Total Deaths : 142,716


My Personal Notes arrow_drop_up
Recommended Articles
Page :