Skip to content

Commit 67ef397

Browse files
authored
fix covid_stats_via_xpath.py
Improve error handling.
1 parent 0ee534e commit 67ef397

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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 Worldometers archive site using lxml.
3+
lxml is chosen over BeautifulSoup for its speed and convenience in Python web projects (such as Django or Flask).
54
"""
65

76
# /// script
@@ -12,28 +11,40 @@
1211
# ]
1312
# ///
1413

15-
from typing import NamedTuple
1614

15+
from typing import NamedTuple
1716
import httpx
1817
from lxml import html
1918

20-
2119
class CovidData(NamedTuple):
2220
cases: str
2321
deaths: str
2422
recovered: str
2523

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

0 commit comments

Comments
 (0)