Skip to content
Open
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
218 changes: 218 additions & 0 deletions web-scraping-single-page.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"from bs4 import BeautifulSoup\n",
"\n",
"import requests\n",
"\n",
"url = 'https://www.billboard.com/charts/hot-100/'\n",
"\n",
"response = requests.get(url)\n",
"response"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"soup = BeautifulSoup(response.content, 'html.parser')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"artist_tags = soup.find_all('span', class_ = 'lrv-u-font-size-14@mobile-max')\n",
"\n",
"artists = []\n",
"for artist in artist_tags:\n",
" artists.append(artist.getText().strip())\n",
" \n",
"len(artists)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({\n",
" 'song': songs,\n",
" 'artist': artists\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"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>song</th>\n",
" <th>artist</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Additional Awards</td>\n",
" <td>Shaboozey</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Imprint/Promotion Label:</td>\n",
" <td>Post Malone Featuring Morgan Wallen</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Imprint/Promotion Label:</td>\n",
" <td>Kendrick Lamar</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Imprint/Promotion Label:</td>\n",
" <td>Sabrina Carpenter</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Imprint/Promotion Label:</td>\n",
" <td>Tommy Richman</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>95</th>\n",
" <td>Imprint/Promotion Label:</td>\n",
" <td>David Guetta &amp; OneRepublic</td>\n",
" </tr>\n",
" <tr>\n",
" <th>96</th>\n",
" <td>Imprint/Promotion Label:</td>\n",
" <td>Taylor Swift</td>\n",
" </tr>\n",
" <tr>\n",
" <th>97</th>\n",
" <td>Imprint/Promotion Label:</td>\n",
" <td>Billie Eilish</td>\n",
" </tr>\n",
" <tr>\n",
" <th>98</th>\n",
" <td>Imprint/Promotion Label:</td>\n",
" <td>Charli XCX With Lorde</td>\n",
" </tr>\n",
" <tr>\n",
" <th>99</th>\n",
" <td>Imprint/Promotion Label:</td>\n",
" <td>The Red Clay Strays</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>100 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" song artist\n",
"0 Additional Awards Shaboozey\n",
"1 Imprint/Promotion Label: Post Malone Featuring Morgan Wallen\n",
"2 Imprint/Promotion Label: Kendrick Lamar\n",
"3 Imprint/Promotion Label: Sabrina Carpenter\n",
"4 Imprint/Promotion Label: Tommy Richman\n",
".. ... ...\n",
"95 Imprint/Promotion Label: David Guetta & OneRepublic\n",
"96 Imprint/Promotion Label: Taylor Swift\n",
"97 Imprint/Promotion Label: Billie Eilish\n",
"98 Imprint/Promotion Label: Charli XCX With Lorde\n",
"99 Imprint/Promotion Label: The Red Clay Strays\n",
"\n",
"[100 rows x 2 columns]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}