From 9d6293f7272f4ffb97cb4d2004b7af4a37927d64 Mon Sep 17 00:00:00 2001 From: santoshrajkumar Date: Sat, 12 Sep 2020 00:43:12 +0530 Subject: [PATCH 1/4] lxmlCovidDataFetch --- .../worldometers_covid_with_lxml.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 web_programming/worldometers_covid_with_lxml.py diff --git a/web_programming/worldometers_covid_with_lxml.py b/web_programming/worldometers_covid_with_lxml.py new file mode 100644 index 000000000000..9e1ac9139bc8 --- /dev/null +++ b/web_programming/worldometers_covid_with_lxml.py @@ -0,0 +1,18 @@ +import requests +from lxml import html + +try: + wld = requests.get("https://www.worldometers.info/coronavirus/") + wldh = html.fromstring(wld.content) + wldc = wldh.xpath('//div[@class = "maincounter-number"]/span/text()') + wcase = wldc[0] + wdeath = wldc[1] + wcure = wldc[2] +except: + wcase = 'fetching.....' + wdeath ='fetching.....' + wcure = 'fetching.....' + +print('Total COVID19 Cases in the world :' + wcase) +print('Total Deaths Due to COVID19 in the world :' + wdeath) +print('Total COVID19 Patients Cured in the world :' + wcure) \ No newline at end of file From 25239cc65ddf778e7f2ea54bc3986c5570d82e43 Mon Sep 17 00:00:00 2001 From: santoshrajkumar Date: Sat, 12 Sep 2020 00:59:52 +0530 Subject: [PATCH 2/4] lxmlCovidDataFetch1 --- .../worldometers_covid_with_lxml.py | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/web_programming/worldometers_covid_with_lxml.py b/web_programming/worldometers_covid_with_lxml.py index 9e1ac9139bc8..5073854e352d 100644 --- a/web_programming/worldometers_covid_with_lxml.py +++ b/web_programming/worldometers_covid_with_lxml.py @@ -1,17 +1,19 @@ +""" +This is to show simple COVID19 info fetching from worldometers site using lxml +* The main motivation to use lxml in place of bs4 is that it is faster & therefore more +convenient to use in Python web projects (e.g. Django based) +""" + import requests from lxml import html -try: - wld = requests.get("https://www.worldometers.info/coronavirus/") - wldh = html.fromstring(wld.content) - wldc = wldh.xpath('//div[@class = "maincounter-number"]/span/text()') - wcase = wldc[0] - wdeath = wldc[1] - wcure = wldc[2] -except: - wcase = 'fetching.....' - wdeath ='fetching.....' - wcure = 'fetching.....' + +wld = requests.get("https://www.worldometers.info/coronavirus/") +wldh = html.fromstring(wld.content) +wldc = wldh.xpath('//div[@class = "maincounter-number"]/span/text()') +wcase = wldc[0] +wdeath = wldc[1] +wcure = wldc[2] print('Total COVID19 Cases in the world :' + wcase) print('Total Deaths Due to COVID19 in the world :' + wdeath) From 0fe06812c7f781cfaf0fc9de3b8149234df6e822 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 11 Sep 2020 22:22:28 +0200 Subject: [PATCH 3/4] Update worldometers_covid_with_lxml.py --- .../worldometers_covid_with_lxml.py | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/web_programming/worldometers_covid_with_lxml.py b/web_programming/worldometers_covid_with_lxml.py index 5073854e352d..d22ed017878c 100644 --- a/web_programming/worldometers_covid_with_lxml.py +++ b/web_programming/worldometers_covid_with_lxml.py @@ -1,20 +1,23 @@ """ This is to show simple COVID19 info fetching from worldometers site using lxml -* The main motivation to use lxml in place of bs4 is that it is faster & therefore more -convenient to use in Python web projects (e.g. Django based) +* The main motivation to use lxml in place of bs4 is that it is faster and therefore +more convenient to use in Python web projects (e.g. Django or Flask-based) """ +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)) -wld = requests.get("https://www.worldometers.info/coronavirus/") -wldh = html.fromstring(wld.content) -wldc = wldh.xpath('//div[@class = "maincounter-number"]/span/text()') -wcase = wldc[0] -wdeath = wldc[1] -wcure = wldc[2] -print('Total COVID19 Cases in the world :' + wcase) -print('Total Deaths Due to COVID19 in the world :' + wdeath) -print('Total COVID19 Patients Cured in the world :' + wcure) \ No newline at end of file +fmt = """Total COVID-19 cases in the world: {} +Total deaths due to COVID-19 in the world: {} +Total COVID-19 patients recovered in the world: {}""" +print(fmt.format(*covid_stats())) From fc693138f19480648532253a991cddda7135f810 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 11 Sep 2020 22:23:25 +0200 Subject: [PATCH 4/4] Rename worldometers_covid_with_lxml.py to covid_stats_via_xpath.py --- .../{worldometers_covid_with_lxml.py => covid_stats_via_xpath.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename web_programming/{worldometers_covid_with_lxml.py => covid_stats_via_xpath.py} (100%) diff --git a/web_programming/worldometers_covid_with_lxml.py b/web_programming/covid_stats_via_xpath.py similarity index 100% rename from web_programming/worldometers_covid_with_lxml.py rename to web_programming/covid_stats_via_xpath.py