Signup/Sign In

Scraping COVID-19 Data using Python BeautifulSoup

Posted in Programming   LAST UPDATED: SEPTEMBER 23, 2021

    COVID-19 has become a pandemic and has gripped the entire world killing about 100k people(at the time of writing this post, i.e. till 10th April 2020) and more than 1.6 million people are infected by it.

    During this pandemic, most of the countries around the world are under lockdown and news channels have become the primary source of information. But there are many websites like Worldometer also publishes updated numbers of total affected, killed and cured patients on its website which you can use to stay updated.

    But as we are techie, why should we go and open the website again and again, when we can write a simple python script to scrape the data from the website and inform us.

    scraping covid-19 data from worldometer website

    So in this tutorial, we will write a simple python script using the requests and BeautifulSoup modules to scrape the update of the COVID-19 case from the Worldometer website.

    To write this script we will require the above mentioned two modules:

    1. requests module

    2. BeautifulSoup module

    If you have these installed in your Python installation, you can directly move on to the script part, but if you don't then run the below pip command to install these modules:

    pip install requests
    
    pip install bs4

    We have already covered how to use requests module in python to send HTTP request just like a browser. We will be doing the same here as well.

    So here is the Python script, you can run this script to see it fetch the live data from the Worldometer website.

    In the script above,

    1. We first initiated an HTTP request to the Worldometer website's webpage to get the HTML document in response.

    2. Then we created the BeautifulSoup object, providing it with the HTML response text and specifying the parser as html.parser

    3. On the Worldometer website, if you use the Chrome's Developer Tools to inspect the element showing the number of cases, you will find that it's a div tag with class maincounter-number which we are using to get the data. You can see in the image below that there are 3 div tags with this class, containing data for total number of cases, deaths and recovered patients respectively.
      Covind 19 data scraping

    Conclusion:

    You can use this script to get the data which can then be displayed on any UI, you can save this data onto a file, or may be send an email with the numbers. You can even try making the URL dynamic with a dropdown for country names, which can add the country part to the URL upon selection.

    There is so much you can do. Fire up your creativity!

    If you have any doubts, comment down below and if you like this post, then show your love by sharing and commenting.

    You may also like:

    About the author:
    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    Tags:BeautifulSoupPythonWeb ScrapingCOVID-19
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS