MCPcopy
hub / github.com/TheAlgorithms/Python / covid_stats

Function covid_stats

web_programming/covid_stats_via_xpath.py:28–50  ·  view source on GitHub ↗
(
    url: str = (
        "https://web.archive.org/web/20250825095350/"
        "https://www.worldometers.info/coronavirus/"
    ),
)

Source from the content-addressed store, hash-verified

26
27
28def covid_stats(
29 url: str = (
30 "https://web.archive.org/web/20250825095350/"
31 "https://www.worldometers.info/coronavirus/"
32 ),
33) -> CovidData:
34 xpath_str = '//div[@class = "maincounter-number"]/span/text()'
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
53if __name__ == "__main__":

Callers 1

Calls 2

CovidDataClass · 0.85
getMethod · 0.45

Tested by

no test coverage detected