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
297 changes: 297 additions & 0 deletions .ipynb_checkpoints/Lab-Web-Scraping-Single-Page-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5da87cb9",
"metadata": {},
"outputs": [],
"source": [
"# Importing library\n",
"import pandas as pd\n",
"import requests\n",
"from bs4 import BeautifulSoup"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "479ead92",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# URL\n",
"url = \"https://www.billboard.com/charts/hot-100/\"\n",
"\n",
"# Get the response\n",
"response = requests.get(url)\n",
"response"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b6d709c6",
"metadata": {},
"outputs": [],
"source": [
"# Getting the soup\n",
"soup = BeautifulSoup(response.content, \"html.parser\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ed4a8144",
"metadata": {},
"outputs": [],
"source": [
"# Finding all by tag and class\n",
"songs = soup.find_all(name = \"h3\", class_ = \"c-title\")\n",
"authors = soup.find_all(name = \"span\", class_ = \"c-label\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "847d3484",
"metadata": {},
"outputs": [],
"source": [
"# Songs\n",
"list_songs = []\n",
"for _ in range(0,100):\n",
" list_songs.append(songs[6+_*4].text.strip())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "85196d0b",
"metadata": {},
"outputs": [],
"source": [
"# Authors\n",
"list_authors_1 = []\n",
"for _ in range(0,14):\n",
" list_authors_1.append(authors[4+_*8].text.strip())\n",
" \n",
"list_authors_2 = []\n",
"for _ in range(0,43):\n",
" list_authors_2.append(authors[118+_*8].text.strip())\n",
" \n",
"list_authors_3 = []\n",
"for _ in range(0,12):\n",
" list_authors_3.append(authors[464+_*8].text.strip())\n",
" \n",
"list_authors_4 = []\n",
"for _ in range(0,24):\n",
" list_authors_4.append(authors[562+_*8].text.strip())\n",
" \n",
"list_authors_5 = []\n",
"for _ in range(0,5):\n",
" list_authors_5.append(authors[756+_*10].text.strip())\n",
" \n",
"list_authors_6 = []\n",
"for _ in range(0,2):\n",
" list_authors_6.append(authors[804+_*8].text.strip())\n",
" \n",
"list_authors = list_authors_1 + list_authors_2 + list_authors_3 + list_authors_4 + list_authors_5 + list_authors_6"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8f29ae88",
"metadata": {},
"outputs": [],
"source": [
"# Ranking\n",
"song_rank = []\n",
"for _ in range(1,101):\n",
" song_rank.append(_)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "293cb875",
"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>rank</th>\n",
" <th>song_name</th>\n",
" <th>author_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": [
" rank 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",
" author_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": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Dataframe\n",
"df = pd.DataFrame({\n",
" \"rank\" : song_rank,\n",
" \"song_name\" : list_songs,\n",
" \"author_name\" : list_authors\n",
"})\n",
"df"
]
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading