Skip to content

Commit 55fadb7

Browse files
authored
fix covid_stats_via_xpath.py typo
1 parent 260dc8f commit 55fadb7

File tree

1 file changed

+16
-16
lines changed

1 file changed

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

67
# /// script
@@ -11,44 +12,43 @@
1112
# ]
1213
# ///
1314

15+
1416
from typing import NamedTuple
17+
1518
import httpx
1619
from lxml import html
1720

18-
1921
class CovidData(NamedTuple):
2022
cases: str
2123
deaths: str
2224
recovered: str
2325

24-
2526
def covid_stats(
26-
url: str = "https://web.archive.org/web/20250825095350/https://www.worldometers.info/coronavirus/",
27+
url: str = (
28+
"https://web.archive.org/web/20250825095350/"
29+
"https://www.worldometers.info/coronavirus/"
30+
),
2731
) -> CovidData:
2832
xpath_str = '//div[@class = "maincounter-number"]/span/text()'
2933
try:
3034
response = httpx.get(url, timeout=10)
3135
response.raise_for_status()
3236
except httpx.TimeoutException:
33-
print(
34-
"Request timed out. Please check your network connection or try again later."
35-
)
37+
print("Request timed out. Please check your network connection or try again later.")
3638
return CovidData("N/A", "N/A", "N/A")
3739
except httpx.HTTPStatusError as e:
3840
print(f"HTTP error occurred: {e}")
3941
return CovidData("N/A", "N/A", "N/A")
40-
except Exception as e:
41-
print(f"An unexpected error occurred: {e}")
42-
return CovidData("N/A", "N/A", "N/A")
4342
data = html.fromstring(response.content).xpath(xpath_str)
4443
if len(data) != 3:
4544
print("Unexpected data format. The page structure may have changed.")
4645
return CovidData("N/A", "N/A", "N/A")
4746
return CovidData(*data)
4847

49-
5048
if __name__ == "__main__":
51-
fmt = """Total COVID-19 cases in the world: {}
52-
Total deaths due to COVID-19 in the world: {}
53-
Total COVID-19 patients recovered in the world: {}"""
54-
print(fmt.format(*covid_stats()))
49+
fmt = (
50+
"Total COVID-19 cases in the world: {}\n"
51+
"Total deaths due to COVID-19 in the world: {}\n"
52+
"Total COVID-19 patients recovered in the world: {}"
53+
)
54+
print(fmt.format(*covid_stats()))

0 commit comments

Comments
 (0)