Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions web_programming/covid_stats_via_xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
# "lxml",
# ]
# ///
from __future__ import annotations

import argparse
import logging
from typing import NamedTuple

import httpx
Expand All @@ -33,27 +36,50 @@ def covid_stats(
) -> CovidData:
xpath_str = '//div[@class = "maincounter-number"]/span/text()'
try:
response = httpx.get(url, timeout=10).raise_for_status()
response = httpx.get(url, timeout=10)
response.raise_for_status()
except httpx.TimeoutException:
print(
logging.error(
"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}")
logging.error(f"HTTP error occurred: {e}")
return CovidData("N/A", "N/A", "N/A")
data = html.fromstring(response.content).xpath(xpath_str)
data: list[str] = html.fromstring(response.content).xpath(xpath_str)
if len(data) != 3:
print("Unexpected data format. The page structure may have changed.")
data = "N/A", "N/A", "N/A"
logging.warning("Unexpected data format. The page structure may have changed.")
return CovidData("N/A", "N/A", "N/A")

return CovidData(*data)


if __name__ == "__main__":
def main() -> None:
"""CLI entry point."""
parser = argparse.ArgumentParser(
description="Fetch COVID-19 statistics from Worldometers (archived)."
)
parser.add_argument(
"--url",
type=str,
default=(
"https://web.archive.org/web/20250825095350/"
"https://www.worldometers.info/coronavirus/"
),
help="Custom archive URL (default: latest snapshot).",
)
# args = parser.parse_args()
args, _ = parser.parse_known_args()

stats = covid_stats(args.url)
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()))
print(fmt.format(*stats))


if __name__ == "__main__":
main()