w3resource

Python Projects: Show simple Covid19 info fetching from worldometers site using lxml

Python Web Project-2 with Solution

Worldometer manually analyzes, validates, and aggregates data from thousands of sources in real time and provides global Covid-19 live statistics for a wide audience of caring people around the world.
The coronavirus Covid-19 is affecting 219 countries and territories around the world and 2 international conveyances. The day is reset after midnight GMT+0. The list of countries and territories and their continental regional classification is based on the United Nations Geoscheme. Sources are provided under "Latest Updates".

Source: https://www.worldometers.info/coronavirus/

Create a Python project to get total Covid-19 cases, total deaths due to Covid-19, total Covid-19 patients recovered in the world.

Sample Output:

Total COVID-19 cases in the world: 99,857,603 

Total deaths due to COVID-19 in the world: 2,140,768

Total COVID-19 patients recovered in the world: 71,859,345

Sample Solution:

Python Code:

#Source: https://bit.ly/3cepXbI
from collections import namedtuple
import requests
from lxml import html

covid_data = namedtuple("covid_data", "cases deaths recovered")
def covid_stats(url: str = "https://www.worldometers.info/coronavirus/") -> covid_data:
    xpath_str = '//div[@class = "maincounter-number"]/span/text()'
    return covid_data(*html.fromstring(requests.get(url).content).xpath(xpath_str))
fmt = """\nTotal COVID-19 cases in the world: {}
\nTotal deaths due to COVID-19 in the world: {}
\nTotal COVID-19 patients recovered in the world: {}"""
print(fmt.format(*covid_stats())) 

Flowchart:

Flowchart: Show simple COVID19 info fetching from worldometers site using lxml.

 

Improve this sample solutions and post your code through Disqus



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/python/web-programming/python-web-programming-2.php