loading...
Cover image for Covid19 live tracker

Covid19 live tracker

sunilaleti profile image Sunil Aleti Updated on ・2 min read

GitHub logo aletisunil / Covid19_liveTracker

Scraps data from website, regarding Covid19 status

Requirements for scraping a site

  • Requests - Used to send all kinds of HTTP requests
  • BeautifulSoup - Used for pulling data out of HTML and XML files
  • lxml - Which allows easy handling of HTML and XML files

In this article, we will see how we scraped worldometers.info

1) First, you need to install all required modules for scraping a site
2) Open your favourite text editor, In my case I used VSCODE
3) Now let's code, First import requests, and bs4 modules by using import keyword

import requests
import bs4
country_name=input("Enter the Country name: ")
def covid19(country):
    res = requests.get("https://www.worldometers.info/coronavirus/#countries")
    soup = bs4.BeautifulSoup(res.text, 'lxml')
    index = -1
    data=soup.select('tr td')
    for i in range(len(data)):
        if data[i].text.lower()==country.lower():
            index=i
            break

    for i in range(7):
        if i == 0:
            print("\nCountry name: "+str(data[i+index].text))
        elif i == 1:
            print("Total cases: "+str(data[i+index].text))
        elif i == 2:
            if data[i+index].text == '':
                print("New cases: 0")
            else:
                print("New cases: "+str(data[i+index].text))
        elif i == 3:
            print("Total deaths: "+str(data[i+index].text))
        elif i == 4:
            if data[i+index].text == '':
                print("New deaths: 0")
            else:
                print("New deaths: "+str(data[i+index].text))
        elif i == 5:
            print("Total Recovered: "+str(data[i+index].text))
        elif i == 6:
            print("Active cases: "+str(data[i+index].text),end='\n\n')
covid19(country_name)

4) Now save this code, in my case i saved it with "Covid19_liveTracker.py"
5) Now you can execute this python script by typing
python Covid19_liveTracker.py

Alt Text

let me know, if you face any errors or problems, Happy Quarantine
Stay Home, Stay Safe.
Hope this problem gets resolves very soon :)

Discussion

pic
Editor guide
Collapse
thewils_alan profile image
Alan Wilson

Sorry, since it didn't work out of the box for me - I was running Python 2.7.17 - I had to play around with it for a while to remove the syntax errors...and...well, one thing led to another :) Still, I learned quite a bit here including how to post code. Never done anything in python before so here goes (without my comments to keep the code clean):

import requests
import bs4

def covid19(country):
    res = requests.get("https://www.worldometers.info/coronavirus/#countries")
    soup = bs4.BeautifulSoup(res.text, 'lxml')
    data=soup.select('tr td')

    index = find_country(data,country)

    if index == -1:
        print("This country does not exist\n\n")
    else:
        Dict = load_country_data(data, index)
        print_country_data(Dict)

def find_country(data, country):
    if country.lower() == "world":
        country = "Total:"

    for i in range(len(data)):
        if data[i].text.lower()==country.lower():
            return i
    return -1

def load_country_data(data, index):
    Dict = {'name': data[index].text,
            'tot_cases': get_int(data[index+1].text),
            'new_cases': get_int(data[index+2].text),
            'tot_deaths': get_int(data[index+3].text),
            'new_deaths': get_int(data[index+4].text),
            'recovered': get_int(data[index+5].text),
            'active': get_int(data[index+6].text)}
    return Dict

def print_country_data(country_data):
    print("Found \n")
    print(country_data['name'])
    print('=' * len(country_data['name']))

    print("Total Cases    : {0:9,d}".format(country_data['tot_cases']))
    print("New   Cases    : {0:+9,d}".format(country_data['new_cases']))

    print("Total Deaths   : {0:9,d}".format(country_data['tot_deaths']))
    print("New   Deaths   : {0:+9,d}".format(country_data['new_deaths']))

    print("Total Recovered: {0:9,d}".format(country_data['recovered']))
    print("Active Cases   : {0:9,d}".format(country_data['active']))

    print("\n\n")

def get_int(str):
    s = str.strip()
    return int(s.replace(',','').replace('+','')) if s else 0

country_name=raw_input("Enter the Country name: ")
covid19(country_name)
Collapse
salahyoumir profile image
salah youmir

Thank you,
I would add a small elif to manage the case of a wrong country name input.
put the country_name=input("Enter the Country name: ") inside the function
and add an else statement :

else:
print("This country doesn't exist")
covid19()

Thank you for this article, looking forward to read more from you

Collapse
sunilaleti profile image
Sunil Aleti Author

Thanks for pointing it out :)

Collapse
rishisundar profile image
Rishi Sundar

Good one. Looking forward to see more from you :)

Collapse
sunilaleti profile image
Sunil Aleti Author

Thanks dude 😊

Collapse
mzaini30 profile image