File tree 1 file changed +23
-0
lines changed
1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change
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 ()))
You can’t perform that action at this time.
0 commit comments