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.
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:
-
requests module
-
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,
-
We first initiated an HTTP request to the Worldometer website's webpage to get the HTML document in response.
-
Then we created the BeautifulSoup object, providing it with the HTML response text and specifying the parser as html.parser
-
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.
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: