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
260 changes: 252 additions & 8 deletions lab-web-scraping.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,267 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "40359eee-9cd7-4884-bfa4-83344c222305",
"metadata": {
"id": "40359eee-9cd7-4884-bfa4-83344c222305"
},
"execution_count": 43,
"id": "e036e796",
"metadata": {},
"outputs": [],
"source": [
"# Your solution goes here"
"import requests\n",
"from bs4 import BeautifulSoup\n",
"soup = BeautifulSoup(response.content, \"html.parser\")\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "8a3e89d0",
"metadata": {},
"outputs": [],
"source": [
"# Bucle para las 10 páginas\n",
"prices = []\n",
"reviews = []\n",
"urls = []\n",
"min_rating = [\"Four\", \"Five\"]\n",
"max_price = 20\n",
"\n",
"for plp in range(1, 5):\n",
" url = f\"https://books.toscrape.com/catalogue/page-{plp}.html\" # ← usar 'plp'\n",
" response = requests.get(url)\n",
" soup = BeautifulSoup(response.content, \"html.parser\")\n",
"\n",
" # Your solution goes here\n",
"\n",
" for price in soup.find_all(\"p\", class_=\"price_color\"):\n",
" value = float(price.text.replace(\"£\", \"\"))\n",
" prices.append(value)\n",
"\n",
" for review in soup.find_all(\"p\", class_=\"star-rating\"):\n",
" value = review.get(\"class\")[1]\n",
" reviews.append(value)\n",
"\n",
" for url in soup.find_all(\"article\", class_=\"product_pod\"):\n",
" value = url.find(\"h3\").a[\"href\"]\n",
" urls.append(value)\n",
"\n",
"\n",
"def scrape_books(min_rating, max_price):\n",
" books_data = []\n",
" for price, review, url in zip(prices, reviews, urls): # recorre a la vez\n",
" if price <= max_price:\n",
" if review in min_rating:\n",
" response = requests.get(\"https://books.toscrape.com/catalogue/\"+ url)\n",
" detail_soup = BeautifulSoup(response.text, \"html.parser\")\n",
"\n",
"\n",
" book_upc = detail_soup.find(\"th\", text=\"UPC\").find_next(\"td\").text if detail_soup.find(\"th\", text=\"UPC\") else None\n",
"\n",
" book_title = detail_soup.find(\"h1\").text if detail_soup.find(\"h1\") else None\n",
"\n",
" book_price = (\n",
" detail_soup.find(\"p\", class_=\"price_color\").text.strip().replace(\"£\", \"\").replace(\"Â\", \"\")\n",
" if detail_soup.find(\"p\", class_=\"price_color\") else None\n",
" )\n",
"\n",
" book_rating = (\n",
" detail_soup.find(\"p\", class_=\"star-rating\").get(\"class\")[1]\n",
" if detail_soup.find(\"p\", class_=\"star-rating\") else None\n",
" )\n",
"\n",
" book_genre = (\n",
" detail_soup.find(\"ul\", class_=\"breadcrumb\").find_all(\"a\")[1].text\n",
" if detail_soup.find(\"ul\", class_=\"breadcrumb\") else None\n",
" )\n",
"\n",
" book_availability = (\n",
" detail_soup.find(\"p\", class_=\"instock availability\").get_text(strip=True)\n",
" .replace(\"(\", \"\").replace(\")\", \"\").replace(\"In stock\", \"\").strip()\n",
" if detail_soup.find(\"p\", class_=\"instock availability\") else None\n",
" )\n",
"\n",
" book_description = (\n",
" detail_soup.find(\"div\", id=\"product_description\").find_next(\"p\").text\n",
" if detail_soup.find(\"div\", id=\"product_description\") else None\n",
" )\n",
"\n",
" \n",
" book = {\n",
" \"upc\": book_upc,\n",
" \"title\": book_title,\n",
" \"price\": book_price,\n",
" \"rating\": book_rating,\n",
" \"genre\": book_genre,\n",
" \"availability\": book_availability,\n",
" \"description\": book_description,\n",
" }\n",
"\n",
" books_data.append(book)\n",
"\n",
" df = pd.DataFrame(books_data)\n",
" return df\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "9c0f8bea",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\MartínPazYáñez\\AppData\\Local\\Temp\\ipykernel_6408\\874691255.py:37: DeprecationWarning: The 'text' argument to find()-type methods is deprecated. Use 'string' instead.\n",
" book_upc = detail_soup.find(\"th\", text=\"UPC\").find_next(\"td\").text if detail_soup.find(\"th\", text=\"UPC\") else None\n"
]
},
{
"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>Five</td>\n",
" <td>Books</td>\n",
" <td>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>Five</td>\n",
" <td>Books</td>\n",
" <td>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>Five</td>\n",
" <td>Books</td>\n",
" <td>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>Four</td>\n",
" <td>Books</td>\n",
" <td>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>Four</td>\n",
" <td>Books</td>\n",
" <td>16 available</td>\n",
" <td>Every summer, Rose goes with her mom and dad t...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>709822d0b5bcb7f4</td>\n",
" <td>Thirst</td>\n",
" <td>17.27</td>\n",
" <td>Five</td>\n",
" <td>Books</td>\n",
" <td>16 available</td>\n",
" <td>On a searing summer Friday, Eddie Chapman has ...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" upc title price \\\n",
"0 ce6396b0f23f6ecc Set Me Free 17.46 \n",
"1 6258a1f6a6dcfe50 The Four Agreements: A Practical Guide to Pers... 17.66 \n",
"2 6be3beb0793a53e7 Sophie's World 15.94 \n",
"3 657fe5ead67a7767 Untitled Collection: Sabbath Poems 2014 14.27 \n",
"4 51653ef291ab7ddc This One Summer 19.49 \n",
"5 709822d0b5bcb7f4 Thirst 17.27 \n",
"\n",
" rating genre availability \\\n",
"0 Five Books 19 available \n",
"1 Five Books 18 available \n",
"2 Five Books 18 available \n",
"3 Four Books 16 available \n",
"4 Four Books 16 available \n",
"5 Five Books 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... \n",
"5 On a searing summer Friday, Eddie Chapman has ... "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df = scrape_books([\"Four\", \"Five\"], 20)\n",
"display(df)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "726251e1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -140,7 +384,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.7"
}
},
"nbformat": 4,
Expand Down