Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5ca2ba69-6901-461f-a842-b8b817991528",
"metadata": {},
"source": [
"Instructions - Scraping popular songs\n",
"\n",
"Your product will take a song as an input from the user and will output another song (the recommendation). In most cases, the recommended song will have to be similar to the inputted song, but the CTO thinks that if the song is on the top charts at the moment, the user will enjoy more a recommendation of a song that's also popular at the moment.\n",
"\n",
"You have find data on the internet about currently popular songs. Billboard maintains a weekly Top 100 of \"hot\" songs here: https://www.billboard.com/charts/hot-100.\n",
"\n",
"It's a good place to start! Scrape the current top 100 songs and their respective artists, and put the information into a pandas dataframe."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bd3e477f-a994-42ee-8178-6085cac6a6af",
"metadata": {},
"outputs": [],
"source": [
"url= 'https://www.billboard.com/charts/hot-100'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8ac8d43b-653c-4797-86d6-4c4bd66e4ff0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import requests\n",
"response= requests.get(url)\n",
"response"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dca45ad1-dbf5-46c8-90e9-e0393012c8a9",
"metadata": {},
"outputs": [],
"source": [
"from bs4 import BeautifulSoup\n",
"\n",
"\n",
"soup= BeautifulSoup(response.content, \"html.parser\") "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "450d61ce-f0db-469d-b8c6-4edaac2c068d",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Ranking</th>\n",
" <th>Song_name</th>\n",
" <th>Artist_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>Lovin On Me</td>\n",
" <td>Jack Harlow</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>Cruel Summer</td>\n",
" <td>Taylor Swift</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Greedy</td>\n",
" <td>Tate McRae</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>Lose Control</td>\n",
" <td>Teddy Swims</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>I Remember Everything</td>\n",
" <td>Zach Bryan Featuring Kacey Musgraves</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>95</th>\n",
" <td>96</td>\n",
" <td>Sensational</td>\n",
" <td>Chris Brown Featuring Davido &amp; Lojay</td>\n",
" </tr>\n",
" <tr>\n",
" <th>96</th>\n",
" <td>97</td>\n",
" <td>When It Comes To You</td>\n",
" <td>Fridayy</td>\n",
" </tr>\n",
" <tr>\n",
" <th>97</th>\n",
" <td>98</td>\n",
" <td>IDGAF</td>\n",
" <td>Tee Grizzley, Mariah The Scientist &amp; Chris Brown</td>\n",
" </tr>\n",
" <tr>\n",
" <th>98</th>\n",
" <td>99</td>\n",
" <td>Save Me The Trouble</td>\n",
" <td>Dan + Shay</td>\n",
" </tr>\n",
" <tr>\n",
" <th>99</th>\n",
" <td>100</td>\n",
" <td>Red Sky</td>\n",
" <td>21 Savage, Tommy Newport &amp; Mikky Ekko</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>100 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" Ranking Song_name \\\n",
"0 1 Lovin On Me \n",
"1 2 Cruel Summer \n",
"2 3 Greedy \n",
"3 4 Lose Control \n",
"4 5 I Remember Everything \n",
".. ... ... \n",
"95 96 Sensational \n",
"96 97 When It Comes To You \n",
"97 98 IDGAF \n",
"98 99 Save Me The Trouble \n",
"99 100 Red Sky \n",
"\n",
" Artist_name \n",
"0 Jack Harlow \n",
"1 Taylor Swift \n",
"2 Tate McRae \n",
"3 Teddy Swims \n",
"4 Zach Bryan Featuring Kacey Musgraves \n",
".. ... \n",
"95 Chris Brown Featuring Davido & Lojay \n",
"96 Fridayy \n",
"97 Tee Grizzley, Mariah The Scientist & Chris Brown \n",
"98 Dan + Shay \n",
"99 21 Savage, Tommy Newport & Mikky Ekko \n",
"\n",
"[100 rows x 3 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"Song_name= []\n",
"Artist_name= []\n",
"\n",
"\n",
"top100 = soup.find_all('h3', class_ ='c-title')\n",
"\n",
"for song in range(6, 404 ,4):\n",
" Song_name.append(top100[song].text.strip())\n",
" \n",
"\n",
"for artist in soup.find_all('span', class_=\"c-label\"):\n",
" Artist_name.append(artist.text.strip())\n",
"\n",
"Artist = [name for name in Artist_name if name not in ['RE-\\nENTRY', '-', 'REENTRY', 'NEW'] and isinstance(name, str) and not name.isdigit()]\n",
" \n",
" \n",
"# create the df\n",
"\n",
"df= pd.DataFrame({'Ranking': list(range(1, 101)) ,'Song_name' : Song_name, 'Artist_name' : Artist})\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "674ab041-6ac1-44f5-9c02-88ec39b5d8d8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading