Track Corona-virus In Your Country with an automated email using Python.
By: Soumya Gupta
Tags: Selenium, Corona, Python

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 webdriver driver = 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.

Account. On the left navigation panel, choose Security.

On the “Signing in to Google ” panel, choose App Passwords . …

” panel, choose . … At the bottom, choose Select app and choose the app you’re using.

and choose the 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.



server=smtplib.SMTP(‘smtp.gmail.com’,587)

server.ehlo()

server.starttls()

server.ehlo()

server.login(‘

subject = ‘Coronavirus stats in your country today!’

body = ‘Today in ‘ + country_name + ‘\



There is new data on coronavirus:\



Total cases: ‘ + total_cases +’\



New cases: ‘ + new_cases + ‘\



Total deaths: ‘ + total_deaths + ‘\



New deaths: ‘ + new_deaths + ‘\



Active cases: ‘ + active_cases + ‘\



Total recovered: ‘ + total_recovered + ‘\



Serious, critical cases: ‘ + serious_critical + ‘\



Check the link:

msg = f”Subject: {subject}



{body}"

server.sendmail(

‘Coronavirus’,

‘

msg

)

print(‘Hey Email has been sent!’)

server.quit() 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(‘ U ser's Email address’,’Generated Password’)subject = ‘Coronavirus stats in your country today!’body = ‘Today in ‘ + country_name + ‘\

There is new data on coronavirus:\

Total cases: ‘ + total_cases +’\

New cases: ‘ + new_cases + ‘\

Total deaths: ‘ + total_deaths + ‘\

New deaths: ‘ + new_deaths + ‘\

Active cases: ‘ + active_cases + ‘\

Total recovered: ‘ + total_recovered + ‘\

Serious, critical cases: ‘ + serious_critical + ‘\

Check the link: https://www.worldometers.info/coronavirus/’ msg = f”Subject: {subject}



{body}"server.sendmail(‘Coronavirus’, U ser's Email address’,msgprint(‘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.