File size: 10,007 Bytes
bacabb7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Web Scraping von Quotes\n",
"\n",
"In diesem Notebook lernen wir, wie man die Website [Quotes to Scrape](https://quotes.toscrape.com/) mit Python und `BeautifulSoup` scrapt. Diese Seite ist ein gutes Beispiel, um das Scraping von Zitaten und Autoren zu üben."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: requests in /home/codespace/.local/lib/python3.12/site-packages (2.32.3)\n",
"Requirement already satisfied: beautifulsoup4 in /home/codespace/.local/lib/python3.12/site-packages (4.12.3)\n",
"Requirement already satisfied: pandas in /home/codespace/.local/lib/python3.12/site-packages (2.2.3)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /home/codespace/.local/lib/python3.12/site-packages (from requests) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /home/codespace/.local/lib/python3.12/site-packages (from requests) (3.10)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /home/codespace/.local/lib/python3.12/site-packages (from requests) (2.2.3)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/codespace/.local/lib/python3.12/site-packages (from requests) (2024.8.30)\n",
"Requirement already satisfied: soupsieve>1.2 in /home/codespace/.local/lib/python3.12/site-packages (from beautifulsoup4) (2.6)\n",
"Requirement already satisfied: numpy>=1.26.0 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from pandas) (1.26.4)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /home/codespace/.local/lib/python3.12/site-packages (from pandas) (2.9.0.post0)\n",
"Requirement already satisfied: pytz>=2020.1 in /home/codespace/.local/lib/python3.12/site-packages (from pandas) (2024.2)\n",
"Requirement already satisfied: tzdata>=2022.7 in /home/codespace/.local/lib/python3.12/site-packages (from pandas) (2024.2)\n",
"Requirement already satisfied: six>=1.5 in /home/codespace/.local/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3 -m pip install --upgrade pip\u001b[0m\n"
]
}
],
"source": [
"# Installieren der benötigten Bibliotheken\n",
"! pip install requests beautifulsoup4 pandas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Schritt 1: HTML-Inhalt abrufen\n",
"\n",
"Wir verwenden die `requests`-Bibliothek, um den HTML-Inhalt der Seite abzurufen."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"HTML-Inhalt erfolgreich abgerufen.\n"
]
}
],
"source": [
"import requests\n",
"\n",
"# URL der Website\n",
"url = \"https://quotes.toscrape.com/\"\n",
"\n",
"# HTML-Inhalt abrufen\n",
"response = requests.get(url)\n",
"\n",
"# Überprüfen, ob die Anfrage erfolgreich war\n",
"if response.status_code == 200:\n",
" print(\"HTML-Inhalt erfolgreich abgerufen.\")\n",
"else:\n",
" print(f\"Fehler beim Abrufen der Seite: {response.status_code}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Schritt 2: HTML mit BeautifulSoup parsen\n",
"\n",
"Jetzt parsen wir den abgerufenen HTML-Inhalt mit `BeautifulSoup`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Quotes to Scrape\n"
]
}
],
"source": [
"from bs4 import BeautifulSoup\n",
"\n",
"# HTML-Inhalt parsen\n",
"soup = BeautifulSoup(response.text, 'html.parser')\n",
"\n",
"# Überprüfen des Titels der Seite\n",
"print(soup.title.string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Schritt 3: Daten extrahieren\n",
"\n",
"Wir extrahieren nun die Zitate und die zugehörigen Autoren."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\"“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”\" - Albert Einstein\n",
"\"“It is our choices, Harry, that show what we truly are, far more than our abilities.”\" - J.K. Rowling\n",
"\"“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”\" - Albert Einstein\n",
"\"“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”\" - Jane Austen\n",
"\"“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.”\" - Marilyn Monroe\n"
]
}
],
"source": [
"# Listen zur Speicherung der Daten\n",
"quotes = []\n",
"authors = []\n",
"\n",
"# Alle Zitatcontainer finden\n",
"quote_blocks = soup.find_all('div', class_='quote')\n",
"\n",
"# Daten extrahieren\n",
"for block in quote_blocks:\n",
" quote = block.find('span', class_='text').text # Zitat\n",
" author = block.find('small', class_='author').text # Autor\n",
" quotes.append(quote)\n",
" authors.append(author)\n",
"\n",
"# Die ersten Zitate anzeigen\n",
"for quote, author in zip(quotes[:5], authors[:5]): # Nur die ersten 5 Zitate\n",
" print(f'\"{quote}\" - {author}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Schritt 4: Daten in einem DataFrame speichern\n",
"\n",
"Um die extrahierten Daten zu speichern, verwenden wir `pandas`, um sie in einem DataFrame zu organisieren."
]
},
{
"cell_type": "code",
"execution_count": 5,
"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>Quote</th>\n",
" <th>Author</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>“The world as we have created it is a process ...</td>\n",
" <td>Albert Einstein</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>“It is our choices, Harry, that show what we t...</td>\n",
" <td>J.K. Rowling</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>“There are only two ways to live your life. On...</td>\n",
" <td>Albert Einstein</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>“The person, be it gentleman or lady, who has ...</td>\n",
" <td>Jane Austen</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>“Imperfection is beauty, madness is genius and...</td>\n",
" <td>Marilyn Monroe</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Quote Author\n",
"0 “The world as we have created it is a process ... Albert Einstein\n",
"1 “It is our choices, Harry, that show what we t... J.K. Rowling\n",
"2 “There are only two ways to live your life. On... Albert Einstein\n",
"3 “The person, be it gentleman or lady, who has ... Jane Austen\n",
"4 “Imperfection is beauty, madness is genius and... Marilyn Monroe"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"# DataFrame erstellen\n",
"quotes_df = pd.DataFrame({\n",
" 'Quote': quotes,\n",
" 'Author': authors\n",
"})\n",
"\n",
"# DataFrame anzeigen\n",
"quotes_df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fazit\n",
"\n",
"In diesem Notebook haben wir gelernt, wie man die Website [Quotes to Scrape](https://quotes.toscrape.com/) mit Python und `BeautifulSoup` scrapt. Wir haben die Zitate und ihre Autoren extrahiert und in einem DataFrame gespeichert."
]
}
],
"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.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|