Skip to content
Closed
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
64 changes: 48 additions & 16 deletions web_programming/world_covid19_stats.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
#!/usr/bin/env python3

"""
Provide the current worldwide COVID-19 statistics.
This data is being scrapped from 'https://www.worldometers.info/coronavirus/'.
"""

import requests
from bs4 import BeautifulSoup


def world_covid19_stats(url: str = "https://www.worldometers.info/coronavirus") -> dict:
def world_covid19_stats(
url: str = "https://www.worldometers.info/coronavirus/",
) -> dict:
"""
Return a dict of current worldwide COVID-19 statistics
Return a dict of current worldwide COVID-19 statistics.

Example:
>>> stats = world_covid19_stats()
>>> isinstance(stats, dict)
True
>>> len(stats) > 0 # Check that we got some statistics
True
>>> 'Total Cases' in stats.keys() # Ensure 'Total Cases' is one of the keys
True

Raises:
Exception: If the request fails or the number of keys and values does not match.
"""
soup = BeautifulSoup(requests.get(url, timeout=10).text, "html.parser")
keys = soup.findAll("h1")
values = soup.findAll("div", {"class": "maincounter-number"})
keys += soup.findAll("span", {"class": "panel-title"})
values += soup.findAll("div", {"class": "number-table-main"})
return {key.text.strip(): value.text.strip() for key, value in zip(keys, values)}
response = requests.get(url, timeout=10)
if response.status_code != 200:
error_message = (
f"Failed to fetch data from {url}, status code: {response.status_code}"
)
raise Exception(error_message)

soup = BeautifulSoup(response.text, "html.parser")
keys = [h1.get_text(strip=True) for h1 in soup.find_all("h1")]
values = [
div.get_text(strip=True)
for div in soup.find_all("div", class_="maincounter-number")
]

keys += [
span.get_text(strip=True)
for span in soup.find_all("span", class_="panel-title")
]
values += [
div.get_text(strip=True)
for div in soup.find_all("div", class_="number-table-main")
]

if len(keys) != len(values):
raise ValueError("The number of keys and values extracted does not match.")

return dict(zip(keys, values))


if __name__ == "__main__":
print("\033[1m COVID-19 Status of the World \033[0m\n")
print("\n".join(f"{key}\n{value}" for key, value in world_covid19_stats().items()))
try:
stats = world_covid19_stats()
print("\n".join(f"{key}\n{value}" for key, value in stats.items()))
except Exception as e:

Check failure on line 57 in web_programming/world_covid19_stats.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (BLE001)

web_programming/world_covid19_stats.py:57:12: BLE001 Do not catch blind exception: `Exception`
print(f"Error fetching COVID-19 stats: {e}")
Loading