D4ve-R commited on
Commit
946e3e9
1 Parent(s): 340bf41

Upload terra_x_scraper.ipynb

Browse files
Files changed (1) hide show
  1. terra_x_scraper.ipynb +378 -0
terra_x_scraper.ipynb ADDED
@@ -0,0 +1,378 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "!pip install google_colab_selenium pandas -q"
21
+ ],
22
+ "metadata": {
23
+ "id": "v6eFzLRMAZY-",
24
+ "colab": {
25
+ "base_uri": "https://localhost:8080/"
26
+ },
27
+ "outputId": "603694b7-378c-48a8-8ec9-8d934ddf7db5"
28
+ },
29
+ "execution_count": 1,
30
+ "outputs": [
31
+ {
32
+ "output_type": "stream",
33
+ "name": "stdout",
34
+ "text": [
35
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m9.5/9.5 MB\u001b[0m \u001b[31m23.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
36
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m467.7/467.7 kB\u001b[0m \u001b[31m14.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
37
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
38
+ "\u001b[?25h"
39
+ ]
40
+ }
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "source": [
46
+ "import json\n",
47
+ "from selenium.webdriver.common.by import By\n",
48
+ "from selenium.webdriver.support.ui import WebDriverWait\n",
49
+ "from selenium.webdriver.support import expected_conditions as EC\n",
50
+ "import google_colab_selenium as gs\n",
51
+ "\n",
52
+ "driver = gs.ChromeDriver()"
53
+ ],
54
+ "metadata": {
55
+ "colab": {
56
+ "base_uri": "https://localhost:8080/",
57
+ "height": 38
58
+ },
59
+ "id": "HuGRNkypD-WR",
60
+ "outputId": "b6d8cca6-28d4-4e86-b7bb-d0e88b28a59f"
61
+ },
62
+ "execution_count": 86,
63
+ "outputs": [
64
+ {
65
+ "output_type": "display_data",
66
+ "data": {
67
+ "text/plain": [
68
+ "<IPython.core.display.HTML object>"
69
+ ],
70
+ "text/html": [
71
+ "\n",
72
+ " <div class=\"spinner-container\">\n",
73
+ " <div class=\"spinner\" id=\"f8a02f04-ae6b-45c4-91fc-344002d27820-circle\"></div>\n",
74
+ " <div class=\"spinner-text\" id=\"f8a02f04-ae6b-45c4-91fc-344002d27820-text\">Initializing Chromedriver</div>\n",
75
+ " </div>\n",
76
+ " <style>\n",
77
+ " @keyframes spin {\n",
78
+ " from { transform: rotate(0deg); }\n",
79
+ " to { transform: rotate(360deg); }\n",
80
+ " }\n",
81
+ "\n",
82
+ " .spinner-container {\n",
83
+ " display: flex;\n",
84
+ " align-items: center;\n",
85
+ " margin-bottom: 3px;\n",
86
+ " }\n",
87
+ "\n",
88
+ " .spinner {\n",
89
+ " border: 3px solid rgba(0, 0, 0, 0.1);\n",
90
+ " border-left-color: lightblue;\n",
91
+ " border-radius: 50%;\n",
92
+ " width: 12px;\n",
93
+ " height: 12px;\n",
94
+ " animation: spin 1s linear infinite;\n",
95
+ " }\n",
96
+ "\n",
97
+ " .spinner-text {\n",
98
+ " padding-left: 6px;\n",
99
+ " }\n",
100
+ " </style>\n",
101
+ " "
102
+ ]
103
+ },
104
+ "metadata": {}
105
+ },
106
+ {
107
+ "output_type": "display_data",
108
+ "data": {
109
+ "text/plain": [
110
+ "<IPython.core.display.Javascript object>"
111
+ ],
112
+ "application/javascript": [
113
+ "\n",
114
+ " const element = document.getElementById(\"f8a02f04-ae6b-45c4-91fc-344002d27820-circle\");\n",
115
+ " element.style.border = \"3px solid limegreen\";\n",
116
+ " element.style.animation = \"none\";\n",
117
+ "\n",
118
+ " const text = document.getElementById(\"f8a02f04-ae6b-45c4-91fc-344002d27820-text\");\n",
119
+ " text.innerText = \"Initialized Chromedriver\";\n",
120
+ " "
121
+ ]
122
+ },
123
+ "metadata": {}
124
+ }
125
+ ]
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "source": [
130
+ "def scrape_categories(url):\n",
131
+ " category = url.split('#')[0].split('/')[-1]\n",
132
+ " items = []\n",
133
+ " # Send a GET request to the website\n",
134
+ " try:\n",
135
+ " driver.get(url)\n",
136
+ " WebDriverWait(driver, 50).until(EC.presence_of_all_elements_located((By.TAG_NAME, \"tx-video-card\")))\n",
137
+ " # Find all <tx-video-card> elements\n",
138
+ " card_elements = driver.find_elements(By.TAG_NAME, \"tx-video-card\")\n",
139
+ " for card in card_elements:\n",
140
+ " url = card.find_element(By.TAG_NAME, 'a').get_attribute('href')\n",
141
+ " title = card.find_element(By.TAG_NAME, 'h3').text\n",
142
+ " items.append({\"url\": url, \"title\": title, \"category\": category})\n",
143
+ "\n",
144
+ " except Exception as e:\n",
145
+ " print(\"An error occurred:\", e)\n",
146
+ " return items"
147
+ ],
148
+ "metadata": {
149
+ "id": "dXTj1qTYU1F_"
150
+ },
151
+ "execution_count": 92,
152
+ "outputs": []
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": 93,
157
+ "metadata": {
158
+ "id": "YHuy1H4aAUp-"
159
+ },
160
+ "outputs": [],
161
+ "source": [
162
+ "def scrape_article(item):\n",
163
+ " # Send a GET request to the website\n",
164
+ " try:\n",
165
+ " driver.get(item['url'])\n",
166
+ " item['text'] = []\n",
167
+ " WebDriverWait(driver, 50).until(EC.presence_of_all_elements_located((By.TAG_NAME, \"p\")))\n",
168
+ "\n",
169
+ " # Find all <p> elements and filter empty elements\n",
170
+ " p_elements = driver.find_elements(By.TAG_NAME, \"p\")\n",
171
+ " p_elements = list(filter(lambda p: p.text, p_elements))\n",
172
+ "\n",
173
+ " # Print the text of each <p> element\n",
174
+ " for p in p_elements:\n",
175
+ " if p.text.strip().startswith('ZDF'):\n",
176
+ " item['attribution'] = p.text.strip()\n",
177
+ " continue\n",
178
+ " if p.text.strip().startswith('http'):\n",
179
+ " item['source'] = p.text.strip()\n",
180
+ "\n",
181
+ " item['text'].append(p.text)\n",
182
+ "\n",
183
+ " except Exception as e:\n",
184
+ " print(\"An error occurred:\", e)\n",
185
+ " return item"
186
+ ]
187
+ },
188
+ {
189
+ "cell_type": "code",
190
+ "source": [
191
+ "urls = [\n",
192
+ " 'https://terraxplaincommons.zdf.de/kategorie/geowissenschaften#videos',\n",
193
+ " 'https://terraxplaincommons.zdf.de/kategorie/der-menschliche-koerper#videos',\n",
194
+ " 'https://terraxplaincommons.zdf.de/kategorie/Biologie#videos',\n",
195
+ " 'https://terraxplaincommons.zdf.de/kategorie/Geschichte#videos',\n",
196
+ " 'https://terraxplaincommons.zdf.de/kategorie/religion#videos',\n",
197
+ " 'https://terraxplaincommons.zdf.de/kategorie/physik#videos',\n",
198
+ " 'https://terraxplaincommons.zdf.de/kategorie/technik#videos',\n",
199
+ " 'https://terraxplaincommons.zdf.de/kategorie/chemie#videos',\n",
200
+ " 'https://terraxplaincommons.zdf.de/kategorie/mathematik#videos',\n",
201
+ " 'https://terraxplaincommons.zdf.de/kategorie/sozialkunde#videos',\n",
202
+ " 'https://terraxplaincommons.zdf.de/kategorie/klima-und-klimawandel#videos',\n",
203
+ " 'https://terraxplaincommons.zdf.de/kategorie/unesco-welterbestaetten#videos'\n",
204
+ "]\n",
205
+ "\n",
206
+ "for url in urls:\n",
207
+ " items = scrape_categories(url)\n",
208
+ " items = map(scrape_article, items)\n",
209
+ " with open('data.jsonl', 'a') as f:\n",
210
+ " for item in items:\n",
211
+ " f.write(json.dumps(item) + '\\n')\n",
212
+ "\n",
213
+ "driver.quit()"
214
+ ],
215
+ "metadata": {
216
+ "id": "quuELC-bdVPG"
217
+ },
218
+ "execution_count": null,
219
+ "outputs": []
220
+ },
221
+ {
222
+ "cell_type": "code",
223
+ "source": [
224
+ "import pandas as pd\n",
225
+ "\n",
226
+ "df = pd.read_json('data.jsonl', lines=True)"
227
+ ],
228
+ "metadata": {
229
+ "id": "IMzB4Qb9in71"
230
+ },
231
+ "execution_count": 233,
232
+ "outputs": []
233
+ },
234
+ {
235
+ "cell_type": "code",
236
+ "source": [
237
+ "print(len(df))\n",
238
+ "# Filter rows where 'url' is null\n",
239
+ "df = df[df['url'].notnull()]\n",
240
+ "# Filter rows where 'attribution' is null\n",
241
+ "df = df[df['attribution'].notnull()]\n",
242
+ "print(len(df))"
243
+ ],
244
+ "metadata": {
245
+ "colab": {
246
+ "base_uri": "https://localhost:8080/"
247
+ },
248
+ "id": "R6QNBS6jkrf9",
249
+ "outputId": "045c92bb-73eb-4ae5-d80b-d45f88b6ce53"
250
+ },
251
+ "execution_count": 234,
252
+ "outputs": [
253
+ {
254
+ "output_type": "stream",
255
+ "name": "stdout",
256
+ "text": [
257
+ "400\n",
258
+ "331\n"
259
+ ]
260
+ }
261
+ ]
262
+ },
263
+ {
264
+ "cell_type": "code",
265
+ "source": [
266
+ "df['text'] = df['text'].apply(lambda arr: [s for s in arr if 'Mehr von Terra X' not in s])\n",
267
+ "df['text'] = df['text'].apply(lambda arr: [s for s in arr if 'https:' not in s])\n",
268
+ "df['text'] = df['text'].apply(lambda arr: [s for s in arr if len(s) >= 20])"
269
+ ],
270
+ "metadata": {
271
+ "id": "qlACB04Vps8Z"
272
+ },
273
+ "execution_count": 235,
274
+ "outputs": []
275
+ },
276
+ {
277
+ "cell_type": "code",
278
+ "source": [
279
+ "# Filter rows with text array length 0\n",
280
+ "df = df[df['text'].apply(len) > 0]\n",
281
+ "print(len(df))"
282
+ ],
283
+ "metadata": {
284
+ "colab": {
285
+ "base_uri": "https://localhost:8080/"
286
+ },
287
+ "id": "nQrdXChExgrc",
288
+ "outputId": "41e65e15-c992-4db1-cd64-ba8924d5db14"
289
+ },
290
+ "execution_count": 236,
291
+ "outputs": [
292
+ {
293
+ "output_type": "stream",
294
+ "name": "stdout",
295
+ "text": [
296
+ "331\n"
297
+ ]
298
+ }
299
+ ]
300
+ },
301
+ {
302
+ "cell_type": "code",
303
+ "source": [
304
+ "# Filter rows with text array length < 3\n",
305
+ "df = df[df['text'].apply(len) < 3]\n",
306
+ "print(len(df))"
307
+ ],
308
+ "metadata": {
309
+ "colab": {
310
+ "base_uri": "https://localhost:8080/"
311
+ },
312
+ "id": "SOKohp8PwQfK",
313
+ "outputId": "6d9ee70a-84e6-406f-bb4d-620a0f7e59f6"
314
+ },
315
+ "execution_count": 237,
316
+ "outputs": [
317
+ {
318
+ "output_type": "stream",
319
+ "name": "stdout",
320
+ "text": [
321
+ "283\n"
322
+ ]
323
+ }
324
+ ]
325
+ },
326
+ {
327
+ "cell_type": "code",
328
+ "source": [
329
+ "# Create new column 'short_text' containing the shorter string from each array\n",
330
+ "df['short_text'] = df['text'].apply(lambda arr: min(arr, key=len))\n",
331
+ "\n",
332
+ "# Delete the 'short_text' from the 'text' array\n",
333
+ "df['text'] = df['text'].apply(lambda arr: max(arr, key=len))\n",
334
+ "\n",
335
+ "# Convert the 'text' array to a string\n",
336
+ "df['text'] = df['text'].str.join('')"
337
+ ],
338
+ "metadata": {
339
+ "id": "9rxgo5uwzXdS"
340
+ },
341
+ "execution_count": 238,
342
+ "outputs": []
343
+ },
344
+ {
345
+ "cell_type": "code",
346
+ "source": [
347
+ "df['source'] = df['source'].fillna('')"
348
+ ],
349
+ "metadata": {
350
+ "id": "lVc_gl8Nz5jO"
351
+ },
352
+ "execution_count": 239,
353
+ "outputs": []
354
+ },
355
+ {
356
+ "cell_type": "code",
357
+ "source": [
358
+ "print(df.head())"
359
+ ],
360
+ "metadata": {
361
+ "id": "gxuik5azzdKl"
362
+ },
363
+ "execution_count": null,
364
+ "outputs": []
365
+ },
366
+ {
367
+ "cell_type": "code",
368
+ "source": [
369
+ "df.to_json('clean_data.jsonl', orient='records', lines=True)"
370
+ ],
371
+ "metadata": {
372
+ "id": "bKzASrQii7su"
373
+ },
374
+ "execution_count": 241,
375
+ "outputs": []
376
+ }
377
+ ]
378
+ }