{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "!pip install google_colab_selenium pandas -q" ], "metadata": { "id": "v6eFzLRMAZY-", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "603694b7-378c-48a8-8ec9-8d934ddf7db5" }, "execution_count": 1, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\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", "\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", "\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", "\u001b[?25h" ] } ] }, { "cell_type": "code", "source": [ "import json\n", "from selenium.webdriver.common.by import By\n", "from selenium.webdriver.support.ui import WebDriverWait\n", "from selenium.webdriver.support import expected_conditions as EC\n", "import google_colab_selenium as gs\n", "\n", "driver = gs.ChromeDriver()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 38 }, "id": "HuGRNkypD-WR", "outputId": "b6d8cca6-28d4-4e86-b7bb-d0e88b28a59f" }, "execution_count": 86, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "\n", "
\n", "
\n", "
Initializing Chromedriver
\n", "
\n", " \n", " " ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "\n", " const element = document.getElementById(\"f8a02f04-ae6b-45c4-91fc-344002d27820-circle\");\n", " element.style.border = \"3px solid limegreen\";\n", " element.style.animation = \"none\";\n", "\n", " const text = document.getElementById(\"f8a02f04-ae6b-45c4-91fc-344002d27820-text\");\n", " text.innerText = \"Initialized Chromedriver\";\n", " " ] }, "metadata": {} } ] }, { "cell_type": "code", "source": [ "def scrape_categories(url):\n", " category = url.split('#')[0].split('/')[-1]\n", " items = []\n", " # Send a GET request to the website\n", " try:\n", " driver.get(url)\n", " WebDriverWait(driver, 50).until(EC.presence_of_all_elements_located((By.TAG_NAME, \"tx-video-card\")))\n", " # Find all elements\n", " card_elements = driver.find_elements(By.TAG_NAME, \"tx-video-card\")\n", " for card in card_elements:\n", " url = card.find_element(By.TAG_NAME, 'a').get_attribute('href')\n", " title = card.find_element(By.TAG_NAME, 'h3').text\n", " items.append({\"url\": url, \"title\": title, \"category\": category})\n", "\n", " except Exception as e:\n", " print(\"An error occurred:\", e)\n", " return items" ], "metadata": { "id": "dXTj1qTYU1F_" }, "execution_count": 92, "outputs": [] }, { "cell_type": "code", "execution_count": 93, "metadata": { "id": "YHuy1H4aAUp-" }, "outputs": [], "source": [ "def scrape_article(item):\n", " # Send a GET request to the website\n", " try:\n", " driver.get(item['url'])\n", " item['text'] = []\n", " WebDriverWait(driver, 50).until(EC.presence_of_all_elements_located((By.TAG_NAME, \"p\")))\n", "\n", " # Find all

elements and filter empty elements\n", " p_elements = driver.find_elements(By.TAG_NAME, \"p\")\n", " p_elements = list(filter(lambda p: p.text, p_elements))\n", "\n", " # Print the text of each

element\n", " for p in p_elements:\n", " if p.text.strip().startswith('ZDF'):\n", " item['attribution'] = p.text.strip()\n", " continue\n", " if p.text.strip().startswith('http'):\n", " item['source'] = p.text.strip()\n", "\n", " item['text'].append(p.text)\n", "\n", " except Exception as e:\n", " print(\"An error occurred:\", e)\n", " return item" ] }, { "cell_type": "code", "source": [ "urls = [\n", " 'https://terraxplaincommons.zdf.de/kategorie/geowissenschaften#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/der-menschliche-koerper#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/Biologie#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/Geschichte#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/religion#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/physik#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/technik#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/chemie#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/mathematik#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/sozialkunde#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/klima-und-klimawandel#videos',\n", " 'https://terraxplaincommons.zdf.de/kategorie/unesco-welterbestaetten#videos'\n", "]\n", "\n", "for url in urls:\n", " items = scrape_categories(url)\n", " items = map(scrape_article, items)\n", " with open('data.jsonl', 'a') as f:\n", " for item in items:\n", " f.write(json.dumps(item) + '\\n')\n", "\n", "driver.quit()" ], "metadata": { "id": "quuELC-bdVPG" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "\n", "df = pd.read_json('data.jsonl', lines=True)" ], "metadata": { "id": "IMzB4Qb9in71" }, "execution_count": 233, "outputs": [] }, { "cell_type": "code", "source": [ "print(len(df))\n", "# Filter rows where 'url' is null\n", "df = df[df['url'].notnull()]\n", "# Filter rows where 'attribution' is null\n", "df = df[df['attribution'].notnull()]\n", "print(len(df))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "R6QNBS6jkrf9", "outputId": "045c92bb-73eb-4ae5-d80b-d45f88b6ce53" }, "execution_count": 234, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "400\n", "331\n" ] } ] }, { "cell_type": "code", "source": [ "df['text'] = df['text'].apply(lambda arr: [s for s in arr if 'Mehr von Terra X' not in s])\n", "df['text'] = df['text'].apply(lambda arr: [s for s in arr if 'https:' not in s])\n", "df['text'] = df['text'].apply(lambda arr: [s for s in arr if len(s) >= 20])" ], "metadata": { "id": "qlACB04Vps8Z" }, "execution_count": 235, "outputs": [] }, { "cell_type": "code", "source": [ "# Filter rows with text array length 0\n", "df = df[df['text'].apply(len) > 0]\n", "print(len(df))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "nQrdXChExgrc", "outputId": "41e65e15-c992-4db1-cd64-ba8924d5db14" }, "execution_count": 236, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "331\n" ] } ] }, { "cell_type": "code", "source": [ "# Filter rows with text array length < 3\n", "df = df[df['text'].apply(len) < 3]\n", "print(len(df))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SOKohp8PwQfK", "outputId": "6d9ee70a-84e6-406f-bb4d-620a0f7e59f6" }, "execution_count": 237, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "283\n" ] } ] }, { "cell_type": "code", "source": [ "# Create new column 'short_text' containing the shorter string from each array\n", "df['short_text'] = df['text'].apply(lambda arr: min(arr, key=len))\n", "\n", "# Delete the 'short_text' from the 'text' array\n", "df['text'] = df['text'].apply(lambda arr: max(arr, key=len))\n", "\n", "# Convert the 'text' array to a string\n", "df['text'] = df['text'].str.join('')" ], "metadata": { "id": "9rxgo5uwzXdS" }, "execution_count": 238, "outputs": [] }, { "cell_type": "code", "source": [ "df['source'] = df['source'].fillna('')" ], "metadata": { "id": "lVc_gl8Nz5jO" }, "execution_count": 239, "outputs": [] }, { "cell_type": "code", "source": [ "print(df.head())" ], "metadata": { "id": "gxuik5azzdKl" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "df.to_json('clean_data.jsonl', orient='records', lines=True)" ], "metadata": { "id": "bKzASrQii7su" }, "execution_count": 241, "outputs": [] } ] }