From 67ef397adb0f531d97c561cc2b6b58269e271190 Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Wed, 17 Sep 2025 21:54:48 +0800 Subject: [PATCH 1/6] fix covid_stats_via_xpath.py Improve error handling. --- web_programming/covid_stats_via_xpath.py | 37 +++++++++++++++--------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/web_programming/covid_stats_via_xpath.py b/web_programming/covid_stats_via_xpath.py index 9c016ba414ea..50dedc0d520d 100644 --- a/web_programming/covid_stats_via_xpath.py +++ b/web_programming/covid_stats_via_xpath.py @@ -1,7 +1,6 @@ """ -This is to show simple COVID19 info fetching from worldometers archive site using lxml -* 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) +This script demonstrates fetching simple COVID-19 statistics from the Worldometers archive site using lxml. +lxml is chosen over BeautifulSoup for its speed and convenience in Python web projects (such as Django or Flask). """ # /// script @@ -12,28 +11,40 @@ # ] # /// -from typing import NamedTuple +from typing import NamedTuple import httpx from lxml import html - class CovidData(NamedTuple): cases: str deaths: str recovered: str - def covid_stats( url: str = "https://web.archive.org/web/20250825095350/https://www.worldometers.info/coronavirus/", ) -> CovidData: xpath_str = '//div[@class = "maincounter-number"]/span/text()' - return CovidData( - *html.fromstring(httpx.get(url, timeout=10).content).xpath(xpath_str) - ) - - -fmt = """Total COVID-19 cases in the world: {} + try: + response = httpx.get(url, timeout=10) + response.raise_for_status() + except httpx.TimeoutException: + print("Request timed out. Please check your network connection or try again later.") + return CovidData("N/A", "N/A", "N/A") + except httpx.HTTPStatusError as e: + print(f"HTTP error occurred: {e}") + return CovidData("N/A", "N/A", "N/A") + except Exception as e: + print(f"An unexpected error occurred: {e}") + return CovidData("N/A", "N/A", "N/A") + data = html.fromstring(response.content).xpath(xpath_str) + if len(data) != 3: + print("Unexpected data format. The page structure may have changed.") + return CovidData("N/A", "N/A", "N/A") + return CovidData(*data) + +if __name__ == "__main__": + 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())) + print(fmt.format(*covid_stats())) From 260dc8f7934f2f0e0848871a9ee0b4de1883fe46 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 17 Sep 2025 13:56:32 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/covid_stats_via_xpath.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/web_programming/covid_stats_via_xpath.py b/web_programming/covid_stats_via_xpath.py index 50dedc0d520d..c783b574ac28 100644 --- a/web_programming/covid_stats_via_xpath.py +++ b/web_programming/covid_stats_via_xpath.py @@ -11,16 +11,17 @@ # ] # /// - from typing import NamedTuple import httpx from lxml import html + class CovidData(NamedTuple): cases: str deaths: str recovered: str + def covid_stats( url: str = "https://web.archive.org/web/20250825095350/https://www.worldometers.info/coronavirus/", ) -> CovidData: @@ -29,7 +30,9 @@ def covid_stats( response = httpx.get(url, timeout=10) response.raise_for_status() except httpx.TimeoutException: - print("Request timed out. Please check your network connection or try again later.") + print( + "Request timed out. Please check your network connection or try again later." + ) return CovidData("N/A", "N/A", "N/A") except httpx.HTTPStatusError as e: print(f"HTTP error occurred: {e}") @@ -43,6 +46,7 @@ def covid_stats( return CovidData("N/A", "N/A", "N/A") return CovidData(*data) + if __name__ == "__main__": fmt = """Total COVID-19 cases in the world: {} Total deaths due to COVID-19 in the world: {} From 55fadb7543aed9fe94da8977a8c73c5ce5caf079 Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Wed, 17 Sep 2025 22:05:34 +0800 Subject: [PATCH 3/6] fix covid_stats_via_xpath.py typo --- web_programming/covid_stats_via_xpath.py | 32 ++++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/web_programming/covid_stats_via_xpath.py b/web_programming/covid_stats_via_xpath.py index c783b574ac28..9c8289ddbbff 100644 --- a/web_programming/covid_stats_via_xpath.py +++ b/web_programming/covid_stats_via_xpath.py @@ -1,6 +1,7 @@ """ -This script demonstrates fetching simple COVID-19 statistics from the Worldometers archive site using lxml. -lxml is chosen over BeautifulSoup for its speed and convenience in Python web projects (such as Django or Flask). +This script demonstrates fetching simple COVID-19 statistics from the Worldometers archive site +using lxml. lxml is chosen over BeautifulSoup for its speed and convenience in Python web projects +(such as Django or Flask). """ # /// script @@ -11,44 +12,43 @@ # ] # /// + from typing import NamedTuple + import httpx from lxml import html - class CovidData(NamedTuple): cases: str deaths: str recovered: str - def covid_stats( - url: str = "https://web.archive.org/web/20250825095350/https://www.worldometers.info/coronavirus/", + url: str = ( + "https://web.archive.org/web/20250825095350/" + "https://www.worldometers.info/coronavirus/" + ), ) -> CovidData: xpath_str = '//div[@class = "maincounter-number"]/span/text()' try: response = httpx.get(url, timeout=10) response.raise_for_status() except httpx.TimeoutException: - print( - "Request timed out. Please check your network connection or try again later." - ) + print("Request timed out. Please check your network connection or try again later.") return CovidData("N/A", "N/A", "N/A") except httpx.HTTPStatusError as e: print(f"HTTP error occurred: {e}") return CovidData("N/A", "N/A", "N/A") - except Exception as e: - print(f"An unexpected error occurred: {e}") - return CovidData("N/A", "N/A", "N/A") data = html.fromstring(response.content).xpath(xpath_str) if len(data) != 3: print("Unexpected data format. The page structure may have changed.") return CovidData("N/A", "N/A", "N/A") return CovidData(*data) - if __name__ == "__main__": - 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())) + fmt = ( + "Total COVID-19 cases in the world: {}\n" + "Total deaths due to COVID-19 in the world: {}\n" + "Total COVID-19 patients recovered in the world: {}" + ) + print(fmt.format(*covid_stats())) \ No newline at end of file From 381c27fe764fa86e96f1e89c918b2f7765b2d8ba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 17 Sep 2025 14:06:13 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/covid_stats_via_xpath.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/web_programming/covid_stats_via_xpath.py b/web_programming/covid_stats_via_xpath.py index 9c8289ddbbff..eb45685a7dec 100644 --- a/web_programming/covid_stats_via_xpath.py +++ b/web_programming/covid_stats_via_xpath.py @@ -12,17 +12,18 @@ # ] # /// - from typing import NamedTuple import httpx from lxml import html + class CovidData(NamedTuple): cases: str deaths: str recovered: str + def covid_stats( url: str = ( "https://web.archive.org/web/20250825095350/" @@ -34,7 +35,9 @@ def covid_stats( response = httpx.get(url, timeout=10) response.raise_for_status() except httpx.TimeoutException: - print("Request timed out. Please check your network connection or try again later.") + print( + "Request timed out. Please check your network connection or try again later." + ) return CovidData("N/A", "N/A", "N/A") except httpx.HTTPStatusError as e: print(f"HTTP error occurred: {e}") @@ -45,10 +48,11 @@ def covid_stats( return CovidData("N/A", "N/A", "N/A") return CovidData(*data) + if __name__ == "__main__": fmt = ( "Total COVID-19 cases in the world: {}\n" "Total deaths due to COVID-19 in the world: {}\n" "Total COVID-19 patients recovered in the world: {}" ) - print(fmt.format(*covid_stats())) \ No newline at end of file + print(fmt.format(*covid_stats())) From efc0d40558e89790ac77180ae87489bdca2eba28 Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Wed, 17 Sep 2025 22:08:41 +0800 Subject: [PATCH 5/6] fix ruff --- web_programming/covid_stats_via_xpath.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/web_programming/covid_stats_via_xpath.py b/web_programming/covid_stats_via_xpath.py index eb45685a7dec..7fb10ef50575 100644 --- a/web_programming/covid_stats_via_xpath.py +++ b/web_programming/covid_stats_via_xpath.py @@ -1,7 +1,8 @@ """ -This script demonstrates fetching simple COVID-19 statistics from the Worldometers archive site -using lxml. lxml is chosen over BeautifulSoup for its speed and convenience in Python web projects -(such as Django or Flask). +This script demonstrates fetching simple COVID-19 statistics from the +Worldometers archive site using lxml. lxml is chosen over BeautifulSoup +for its speed and convenience in Python web projects (such as Django or +Flask). """ # /// script @@ -36,7 +37,8 @@ def covid_stats( response.raise_for_status() except httpx.TimeoutException: print( - "Request timed out. Please check your network connection or try again later." + "Request timed out. Please check your network connection " + "or try again later." ) return CovidData("N/A", "N/A", "N/A") except httpx.HTTPStatusError as e: @@ -44,7 +46,10 @@ def covid_stats( return CovidData("N/A", "N/A", "N/A") data = html.fromstring(response.content).xpath(xpath_str) if len(data) != 3: - print("Unexpected data format. The page structure may have changed.") + print( + "Unexpected data format. The page structure may have " + "changed." + ) return CovidData("N/A", "N/A", "N/A") return CovidData(*data) @@ -55,4 +60,4 @@ def covid_stats( "Total deaths due to COVID-19 in the world: {}\n" "Total COVID-19 patients recovered in the world: {}" ) - print(fmt.format(*covid_stats())) + print(fmt.format(*covid_stats())) \ No newline at end of file From 69459b34983b60db9d2c9f7e609940052fffd3bc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 17 Sep 2025 14:09:02 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/covid_stats_via_xpath.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/web_programming/covid_stats_via_xpath.py b/web_programming/covid_stats_via_xpath.py index 7fb10ef50575..d7ca58d19b2f 100644 --- a/web_programming/covid_stats_via_xpath.py +++ b/web_programming/covid_stats_via_xpath.py @@ -46,10 +46,7 @@ def covid_stats( return CovidData("N/A", "N/A", "N/A") data = html.fromstring(response.content).xpath(xpath_str) if len(data) != 3: - print( - "Unexpected data format. The page structure may have " - "changed." - ) + print("Unexpected data format. The page structure may have changed.") return CovidData("N/A", "N/A", "N/A") return CovidData(*data) @@ -60,4 +57,4 @@ def covid_stats( "Total deaths due to COVID-19 in the world: {}\n" "Total COVID-19 patients recovered in the world: {}" ) - print(fmt.format(*covid_stats())) \ No newline at end of file + print(fmt.format(*covid_stats()))