Skip to content

Commit 4ec71a3

Browse files
fix covid_stats_via_xpath.py (#12975)
* fix covid_stats_via_xpath.py Improve error handling. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix covid_stats_via_xpath.py typo * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix ruff * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * upgrade covid_stats_via_xpath.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update and fix covid_stats_via_xpath.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 0ee534e commit 4ec71a3

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

web_programming/covid_stats_via_xpath.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
2-
This is to show simple COVID19 info fetching from worldometers archive 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)
2+
This script demonstrates fetching simple COVID-19 statistics from the
3+
Worldometers archive site using lxml. lxml is chosen over BeautifulSoup
4+
for its speed and convenience in Python web projects (such as Django or
5+
Flask).
56
"""
67

78
# /// script
@@ -25,15 +26,34 @@ class CovidData(NamedTuple):
2526

2627

2728
def covid_stats(
28-
url: str = "https://web.archive.org/web/20250825095350/https://www.worldometers.info/coronavirus/",
29+
url: str = (
30+
"https://web.archive.org/web/20250825095350/"
31+
"https://www.worldometers.info/coronavirus/"
32+
),
2933
) -> CovidData:
3034
xpath_str = '//div[@class = "maincounter-number"]/span/text()'
31-
return CovidData(
32-
*html.fromstring(httpx.get(url, timeout=10).content).xpath(xpath_str)
35+
try:
36+
response = httpx.get(url, timeout=10).raise_for_status()
37+
except httpx.TimeoutException:
38+
print(
39+
"Request timed out. Please check your network connection "
40+
"or try again later."
41+
)
42+
return CovidData("N/A", "N/A", "N/A")
43+
except httpx.HTTPStatusError as e:
44+
print(f"HTTP error occurred: {e}")
45+
return CovidData("N/A", "N/A", "N/A")
46+
data = html.fromstring(response.content).xpath(xpath_str)
47+
if len(data) != 3:
48+
print("Unexpected data format. The page structure may have changed.")
49+
data = "N/A", "N/A", "N/A"
50+
return CovidData(*data)
51+
52+
53+
if __name__ == "__main__":
54+
fmt = (
55+
"Total COVID-19 cases in the world: {}\n"
56+
"Total deaths due to COVID-19 in the world: {}\n"
57+
"Total COVID-19 patients recovered in the world: {}"
3358
)
34-
35-
36-
fmt = """Total COVID-19 cases in the world: {}
37-
Total deaths due to COVID-19 in the world: {}
38-
Total COVID-19 patients recovered in the world: {}"""
39-
print(fmt.format(*covid_stats()))
59+
print(fmt.format(*covid_stats()))

0 commit comments

Comments
 (0)