(
url: str = (
"https://web.archive.org/web/20250825095350/"
"https://www.worldometers.info/coronavirus/"
),
)
| 26 | |
| 27 | |
| 28 | def 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 | |
| 53 | if __name__ == "__main__": |
no test coverage detected