Skip to content

Commit c676956

Browse files
lxmlCovidDataFetch (TheAlgorithms#2416)
* lxmlCovidDataFetch * lxmlCovidDataFetch1 * Update worldometers_covid_with_lxml.py * Rename worldometers_covid_with_lxml.py to covid_stats_via_xpath.py Co-authored-by: Christian Clauss <[email protected]>
1 parent a191f89 commit c676956

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
This is to show simple COVID19 info fetching from worldometers site using lxml
3+
* The main motivation to use lxml in place of bs4 is that it is faster and therefore
4+
more convenient to use in Python web projects (e.g. Django or Flask-based)
5+
"""
6+
7+
from collections import namedtuple
8+
9+
import requests
10+
from lxml import html
11+
12+
covid_data = namedtuple("covid_data", "cases deaths recovered")
13+
14+
15+
def covid_stats(url: str = "https://www.worldometers.info/coronavirus/") -> covid_data:
16+
xpath_str = '//div[@class = "maincounter-number"]/span/text()'
17+
return covid_data(*html.fromstring(requests.get(url).content).xpath(xpath_str))
18+
19+
20+
fmt = """Total COVID-19 cases in the world: {}
21+
Total deaths due to COVID-19 in the world: {}
22+
Total COVID-19 patients recovered in the world: {}"""
23+
print(fmt.format(*covid_stats()))

0 commit comments

Comments
 (0)