Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,34 +40,36 @@ def get_billboard_hot_100_last_countdown() -> list:
|
|
| 40 |
from bs4 import BeautifulSoup
|
| 41 |
|
| 42 |
url = "https://www.billboard.com/charts/hot-100/"
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
artist_name_elem = item.select("span.c-label")
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
return top_songs
|
| 69 |
-
else:
|
| 70 |
-
raise ValueError(f"Failed to fetch Billboard Hot 100. HTTP Status: {response.status_code}")
|
| 71 |
|
| 72 |
|
| 73 |
|
|
|
|
| 40 |
from bs4 import BeautifulSoup
|
| 41 |
|
| 42 |
url = "https://www.billboard.com/charts/hot-100/"
|
| 43 |
+
try:
|
| 44 |
+
response = requests.get(url)
|
| 45 |
+
response.raise_for_status() # Raise an error for failed request
|
| 46 |
+
except requests.exceptions.RequestException as e:
|
| 47 |
+
raise ValueError(f"Error connecting to Billboard Hot 100: {e}")
|
| 48 |
|
| 49 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 50 |
+
|
| 51 |
+
top_songs = []
|
| 52 |
+
chart_items = soup.select("li.o-chart-results-list__item")
|
| 53 |
+
|
| 54 |
+
for item in chart_items[:10]:
|
| 55 |
+
song_name_elem = item.select_one("h3")
|
| 56 |
+
artist_name_elem = item.select("span.c-label")
|
| 57 |
|
| 58 |
+
if song_name_elem and artist_name_elem:
|
| 59 |
+
song_name = song_name_elem.get_text(strip=True)
|
|
|
|
| 60 |
|
| 61 |
+
# The artist name is usually the second span.c-label element in Billboard's structure
|
| 62 |
+
artist_name = None
|
| 63 |
+
for span in artist_name_elem:
|
| 64 |
+
if not span.get_text(strip=True).isdigit(): # Avoid selecting ranking numbers
|
| 65 |
+
artist_name = span.get_text(strip=True)
|
| 66 |
+
break
|
| 67 |
+
|
| 68 |
+
if artist_name:
|
| 69 |
+
top_songs.append({"song": song_name, "artist": artist_name})
|
| 70 |
+
|
| 71 |
+
return top_songs
|
| 72 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
|