Track Corona-virus In Your Country with an automated email using Python.

Soumya Gupta
Analytics Vidhya
Published in
5 min readMar 18, 2020

Coronaviruses (CoV) are a large family of viruses that cause illness ranging from the common cold to more severe diseases such as Middle East Respiratory Syndrome (MERS-CoV) and Severe Acute Respiratory Syndrome (SARS-CoV).

The incubation period for COVID-19, the disease caused by the novel coronavirus, is 2 to 14 days.

In this post, we’ll track Coronavirus in a particular country and also will receive all the detailed numbers in our inbox using Python.

Let’s dive into this interesting tutorial in a detailed step by step manner.

Step 1. Install Selenium in Python

Selenium is an open-source web-based automation tool. It can send the standard Python commands to different browsers, despite variation in their browser’s design and we’ll use this to fetch the data from the below website.

To install Selenium, please type below in your command prompt, once standard python is installed.

pip install Selenium

Step 2. Downloading Chromedriver for Chrome

Additional to the above step, please download Chromedriver.exe, which will be required in order to open the Chrome browser.

Please download the same from below location as per your own Chrome browser version, also note that 32-bit version supports both 32 and 64-bit machines.

Once completed please open your Jupyter notebook and place your chromedriver.exe file in the same directory where your Jupyter notebook is running.

We can use the below command in any Jupyter cell to identify our current working directory. The final directory structure should be the same as shown in the given image,and we can ignore the checkpoints folder.

## Returns current working directory
pwd()

Step 3. Open the Chrome browser using Selenium.

from selenium import webdriverdriver = webdriver.Chrome(executable_path=”chromedriver.exe”)
driver.maximize_window()
## for waiting the WebElements to load till 30 seconds
driver.implicitly_wait(30)
driver.get(“https://www.worldometers.info/coronavirus/#countries")

The above steps should open the Chrome browser along with navigating to the above web-page.

Step 4. Scraping the web for data corresponding to your country

We’ll be getting the data here for country “India” , you can modify the code for your own country.

country_data=driver.find_elements_by_xpath(“(//*[text()=’ India ‘])[1]/ancestor::tr/td”)

Here I am using XPath to find all the data corresponding to row with the country label India, if you are new to XPath below tutorial will be helpful.

You can verify in the below image, how our Xpath is highlighting all the row data for country “India” from the given table.
We are storing all the corresponding elements of row “India” into a variable country_data which is of type list in Python.

Step 5. Parsing the country_data list and generating the new app password for email function.

Here country_data list contains value corresponding to each column, thus we are separating the information as below for each value.

country_name=country_data[0].text
total_cases=country_data[1].text
new_cases=country_data[2].text
total_deaths=country_data[3].text
new_deaths=country_data[4].text
total_recovered=country_data[5].text
active_cases=country_data[6].text
serious_critical=country_data[7].text
Tot_cases_1M_pop=country_data[8].text

Now let’s generate the app password using below steps in order to send an email using Python’s SMTP library.

Steps to generate the app password for Gmail

  • Go to your Google Account.
  • On the left navigation panel, choose Security.
  • On the “Signing in to Google” panel, choose App Passwords. …
  • At the bottom, choose Select app and choose the app you’re using.
  • Choose Select device and choose the device you’re using.
  • Choose Generate and save the password somewhere.

Step 6. Implementing the email function using Google’s SMTP server

Finally, it’s time to construct our email function using all the parameters from Step 5 and using them in email body after newline formatting.

def send_mail(country_name,total_cases,new_cases,total_death,new_deaths,total_recovered,active_cases,serious_critical,Tot_cases_1M_pop):
server=smtplib.SMTP(‘smtp.gmail.com’,587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(‘User's Email address’,’Generated Password’)
subject = ‘Coronavirus stats in your country today!’
body = ‘Today in ‘ + country_name + ‘\
\nThere is new data on coronavirus:\
\nTotal cases: ‘ + total_cases +’\
\nNew cases: ‘ + new_cases + ‘\
\nTotal deaths: ‘ + total_deaths + ‘\
\nNew deaths: ‘ + new_deaths + ‘\
\nActive cases: ‘ + active_cases + ‘\
\nTotal recovered: ‘ + total_recovered + ‘\
\nSerious, critical cases: ‘ + serious_critical + ‘\
\nCheck the link: https://www.worldometers.info/coronavirus/’
msg = f”Subject: {subject}\n\n{body}"
server.sendmail(
‘Coronavirus’,
User's Email address’,
msg
)
print(‘Hey Email has been sent!’)
server.quit()

In the above function we are using all the predefined stats for sending an e-mail to our designated inbox address with the help of generated app password.

Step 7. Calling the function and verifying the mail in inbox

send_mail(country_name,total_cases,new_cases,total_death,new_deaths,total_recovered,
active_cases,serious_critical,Tot_cases_1M_pop)

Congratulations for receiving the consolidated mail in your inbox.

You can even schedule this script to run daily without any manual intervention by creating a windows batch file and targeting it as a service or running it with the Windows task scheduler.

Thank you for reading till here. In this post, we learned how to scrape the Web for Coronavirus details using Selenium with Python, and how to send email to recipients.

I hope you found this tutorial useful. I’m curious about what you think so hit me with some comments. You can also get in touch with me directly through email or connect with me on LinkedIn.

--

--