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
198 changes: 194 additions & 4 deletions lab-web-scraping.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,213 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "40359eee-9cd7-4884-bfa4-83344c222305",
"metadata": {
"id": "40359eee-9cd7-4884-bfa4-83344c222305"
},
"outputs": [],
"source": [
"# Your solution goes here"
"import requests\n",
"from bs4 import BeautifulSoup\n",
"import pandas as pd\n",
"import re\n",
"\n",
"rating_map = {\n",
" \"One\": 1,\n",
" \"Two\": 2,\n",
" \"Three\": 3,\n",
" \"Four\": 4,\n",
" \"Five\": 5\n",
"}\n",
"\n",
"def scrape_books(min_rating=4, max_price=20):\n",
" base_url = \"https://books.toscrape.com/catalogue/\"\n",
" rows = []\n",
"\n",
" for page in range(1, 51):\n",
" url = f\"https://books.toscrape.com/catalogue/page-{page}.html\"\n",
" r = requests.get(url)\n",
" if r.status_code != 200:\n",
" break\n",
"\n",
" soup = BeautifulSoup(r.text, \"html.parser\")\n",
" books = soup.find_all(\"article\", class_=\"product_pod\")\n",
"\n",
" for b in books:\n",
" try:\n",
" title = b.h3.a[\"title\"]\n",
" link = base_url + b.h3.a[\"href\"]\n",
" rating = rating_map[b.p[\"class\"][1]]\n",
" price = float(re.sub(r\"[^0-9.]\", \"\", b.find(\"p\", class_=\"price_color\").text))\n",
"\n",
" if rating < min_rating or price > max_price:\n",
" continue\n",
"\n",
" det = requests.get(link)\n",
" ds = BeautifulSoup(det.text, \"html.parser\")\n",
"\n",
" upc = ds.find(\"table\").find_all(\"td\")[0].text\n",
" genre = ds.find(\"ul\", class_=\"breadcrumb\").find_all(\"li\")[2].text.strip()\n",
" desc_tag = ds.find(\"div\", id=\"product_description\")\n",
" description = desc_tag.find_next(\"p\").text.strip() if desc_tag else \"\"\n",
" availability = ds.find(\"p\", class_=\"instock availability\").text.strip()\n",
"\n",
" rows.append({\n",
" \"UPC\": upc,\n",
" \"Title\": title,\n",
" \"Price (£)\": price,\n",
" \"Rating\": rating,\n",
" \"Genre\": genre,\n",
" \"Availability\": availability,\n",
" \"Description\": description\n",
" })\n",
"\n",
" except:\n",
" continue\n",
"\n",
" return pd.DataFrame(rows)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ea882728",
"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>UPC</th>\n",
" <th>Title</th>\n",
" <th>Price (£)</th>\n",
" <th>Rating</th>\n",
" <th>Genre</th>\n",
" <th>Availability</th>\n",
" <th>Description</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>ce6396b0f23f6ecc</td>\n",
" <td>Set Me Free</td>\n",
" <td>17.46</td>\n",
" <td>5</td>\n",
" <td>Young Adult</td>\n",
" <td>In stock (19 available)</td>\n",
" <td>Aaron Ledbetter’s future had been planned ou...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>6258a1f6a6dcfe50</td>\n",
" <td>The Four Agreements: A Practical Guide to Pers...</td>\n",
" <td>17.66</td>\n",
" <td>5</td>\n",
" <td>Spirituality</td>\n",
" <td>In stock (18 available)</td>\n",
" <td>In The Four Agreements, don Miguel Ruiz reveal...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6be3beb0793a53e7</td>\n",
" <td>Sophie's World</td>\n",
" <td>15.94</td>\n",
" <td>5</td>\n",
" <td>Philosophy</td>\n",
" <td>In stock (18 available)</td>\n",
" <td>A page-turning novel that is also an explorati...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>657fe5ead67a7767</td>\n",
" <td>Untitled Collection: Sabbath Poems 2014</td>\n",
" <td>14.27</td>\n",
" <td>4</td>\n",
" <td>Poetry</td>\n",
" <td>In stock (16 available)</td>\n",
" <td>More than thirty-five years ago, when the weat...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>51653ef291ab7ddc</td>\n",
" <td>This One Summer</td>\n",
" <td>19.49</td>\n",
" <td>4</td>\n",
" <td>Sequential Art</td>\n",
" <td>In stock (16 available)</td>\n",
" <td>Every summer, Rose goes with her mom and dad t...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" UPC Title \\\n",
"0 ce6396b0f23f6ecc Set Me Free \n",
"1 6258a1f6a6dcfe50 The Four Agreements: A Practical Guide to Pers... \n",
"2 6be3beb0793a53e7 Sophie's World \n",
"3 657fe5ead67a7767 Untitled Collection: Sabbath Poems 2014 \n",
"4 51653ef291ab7ddc This One Summer \n",
"\n",
" Price (£) Rating Genre Availability \\\n",
"0 17.46 5 Young Adult In stock (19 available) \n",
"1 17.66 5 Spirituality In stock (18 available) \n",
"2 15.94 5 Philosophy In stock (18 available) \n",
"3 14.27 4 Poetry In stock (16 available) \n",
"4 19.49 4 Sequential Art In stock (16 available) \n",
"\n",
" Description \n",
"0 Aaron Ledbetter’s future had been planned ou... \n",
"1 In The Four Agreements, don Miguel Ruiz reveal... \n",
"2 A page-turning novel that is also an explorati... \n",
"3 More than thirty-five years ago, when the weat... \n",
"4 Every summer, Rose goes with her mom and dad t... "
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = scrape_books(min_rating=4, max_price=20)\n",
"df.head()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88bac121",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -140,7 +330,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down