{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "f133d1af", "metadata": {}, "source": [ "# Working with Data" ] }, { "attachments": {}, "cell_type": "markdown", "id": "231cb7ec", "metadata": {}, "source": [ "In this notebook, we are going to work on the datasets that are going to be used in order to fine tune our NLP models. \n", "The different steps that we are going to follow are:\n", "\n", "- Scraping ads data from Moroccan youtube products channels" ] }, { "cell_type": "markdown", "id": "8313dd19", "metadata": {}, "source": [ "# Scraping Data" ] }, { "cell_type": "code", "execution_count": 39, "id": "978c782a", "metadata": {}, "outputs": [], "source": [ "from selenium import webdriver\n", "from selenium.webdriver.edge.options import Options\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", "from bs4 import BeautifulSoup\n", "import time\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 52, "id": "5c643463", "metadata": {}, "outputs": [], "source": [ "def scrape_ads(url, channel_name, max_scroll=3, wait_time=2):\n", " options = Options()\n", " options.headless = True\n", " options.add_argument(\"--disable-gpu\")\n", " options.add_argument(\"--no-sandbox\")\n", " \n", " # Mute the audio\n", " options.add_argument(\"--mute-audio\")\n", "\n", " driver = webdriver.Edge(options=options)\n", "\n", " driver.get(url)\n", "\n", " # Scroll down to load more videos\n", " height = driver.execute_script(\"return document.documentElement.scrollHeight\")\n", " while True:\n", " driver.execute_script(\"window.scrollTo(0, document.documentElement.scrollHeight);\")\n", " time.sleep(3) # Allow time for the page to load\n", " new_height = driver.execute_script(\"return document.documentElement.scrollHeight\")\n", " if new_height == height:\n", " time.sleep(5) # Wait a bit longer to check if more content loads\n", " new_height = driver.execute_script(\"return document.documentElement.scrollHeight\")\n", " if new_height == height: # If the height is still the same, we are at the bottom\n", " break\n", " height = new_height\n", "\n", " src = driver.page_source\n", " soup = BeautifulSoup(src, 'lxml')\n", " video_links = soup.find_all('a', {'class': 'yt-simple-endpoint focus-on-expand style-scope ytd-rich-grid-media'})\n", " links = [\"https://www.youtube.com\" + link.get('href') for link in video_links if link.get('href')]\n", "\n", " ads = []\n", " titles = []\n", " for link in links:\n", " driver.get(link)\n", " time.sleep(5)\n", "\n", " src = driver.page_source\n", " soup = BeautifulSoup(src, 'lxml')\n", "\n", " ad_text_span = soup.find('span', class_='yt-core-attributed-string--link-inherit-color')\n", " ad_text = ad_text_span.text.strip() if ad_text_span else \"No ad found\"\n", " ads.append(ad_text)\n", "\n", " title_tag = soup.find('h1', {'class': 'title style-scope ytd-video-primary-info-renderer'})\n", " video_title = title_tag.text.strip() if title_tag else 'Title not found'\n", " titles.append(video_title)\n", "\n", " driver.quit()\n", " return ads, titles, links" ] }, { "cell_type": "code", "execution_count": 63, "id": "939fa26b", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\ADS\\AppData\\Local\\Temp\\ipykernel_14604\\116512856.py:3: DeprecationWarning: headless property is deprecated, instead use add_argument('--headless') or add_argument('--headless=new')\n", " options.headless = True\n" ] } ], "source": [ "channel = '@inwi'\n", "url = f\"https://www.youtube.com/{channel}/videos\"\n", "ads, titles, links = scrape_ads(url, channel)" ] }, { "cell_type": "code", "execution_count": 64, "id": "188b9ba2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1380, 1380, 1380)" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(ads), len(titles), len(links)" ] }, { "cell_type": "code", "execution_count": 65, "id": "d6dedc02", "metadata": {}, "outputs": [], "source": [ "# put ads, titles and links in a dataframe\n", "df3 = pd.DataFrame({'ad': ads, 'title': titles, 'link': links})\n", "\n", "# add channel column\n", "df3['channel'] = channel" ] }, { "cell_type": "code", "execution_count": 67, "id": "c7a6bb24", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
adtitlelinkchannel
1375initiative e-Madrassa proposée par inwi (Souti...النشاط العلمي - حت التربة - الجزء الأول Janvie...https://www.youtube.com/watch?v=9z2eSPv7dLo@inwi
1376initiative e-Madrassa proposée par inwi (Souti...اللغة العربية -أنشطة تطبيقية - الجزء الثاني - ...https://www.youtube.com/watch?v=IgsAKIPPjig@inwi
1377Le présent de l'indicatif des verbes pronominauxLe présent de l'indicatif des verbes pronominauxhttps://www.youtube.com/watch?v=NvYs05Ix3OY@inwi
1378Soutien scolaire - Français - Novembre 2012Soutien scolaire - Français - Novembre 2012https://www.youtube.com/watch?v=ThXZp-jRrUY@inwi
1379Les contes marocains par la voix de Rachi El O...Emadrassa - L'espace de lecture présenté par R...https://www.youtube.com/watch?v=OPaECpKBl6I@inwi
\n", "
" ], "text/plain": [ " ad \n", "1375 initiative e-Madrassa proposée par inwi (Souti... \\\n", "1376 initiative e-Madrassa proposée par inwi (Souti... \n", "1377 Le présent de l'indicatif des verbes pronominaux \n", "1378 Soutien scolaire - Français - Novembre 2012 \n", "1379 Les contes marocains par la voix de Rachi El O... \n", "\n", " title \n", "1375 النشاط العلمي - حت التربة - الجزء الأول Janvie... \\\n", "1376 اللغة العربية -أنشطة تطبيقية - الجزء الثاني - ... \n", "1377 Le présent de l'indicatif des verbes pronominaux \n", "1378 Soutien scolaire - Français - Novembre 2012 \n", "1379 Emadrassa - L'espace de lecture présenté par R... \n", "\n", " link channel \n", "1375 https://www.youtube.com/watch?v=9z2eSPv7dLo @inwi \n", "1376 https://www.youtube.com/watch?v=IgsAKIPPjig @inwi \n", "1377 https://www.youtube.com/watch?v=NvYs05Ix3OY @inwi \n", "1378 https://www.youtube.com/watch?v=ThXZp-jRrUY @inwi \n", "1379 https://www.youtube.com/watch?v=OPaECpKBl6I @inwi " ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df3.tail()" ] }, { "cell_type": "code", "execution_count": 69, "id": "e9f0c14f", "metadata": {}, "outputs": [], "source": [ "# concat df and df1 , df1, df3\n", "df_ads = pd.concat([df, df1, df2, df3], ignore_index=True)" ] }, { "cell_type": "code", "execution_count": 73, "id": "94398c8a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
adtitlelinkchannel
0بشراكة مع بنك التغدية، مؤسسة أورنج تلتزم بالمس...Orange Maroc : خطوة خيرhttps://www.youtube.com/watch?v=3RoNBU-snrc@orangemaroc
1خدمة Transfert internet كاينة مع أورنج 🧡\\n\\nعن...Orange Maroc: Transfert de soldehttps://www.youtube.com/watch?v=oQKRx9i2NdY@orangemaroc
2أورنج ديما كاينة معاك !🧡\\n\\nمع خدمة solde dépa...Orange Maroc: Solde dépannagehttps://www.youtube.com/watch?v=ElW8plKHRrI@orangemaroc
3لفراجة كاينة مع La Fibre d'Orange !\\nنتا غير ...Orange Maroc : La Fibre d'Orange لفراجة كاينة معhttps://www.youtube.com/watch?v=sDt4tK29Vy8@orangemaroc
4سخاوة النجمة 3 المضوبلة كاينة ! 😍\\nاستافدو من ...Orange Maroc : النجمة 3 المضوبلة كاينةhttps://www.youtube.com/watch?v=U2VOn8mhN8U@orangemaroc
\n", "
" ], "text/plain": [ " ad \n", "0 بشراكة مع بنك التغدية، مؤسسة أورنج تلتزم بالمس... \\\n", "1 خدمة Transfert internet كاينة مع أورنج 🧡\\n\\nعن... \n", "2 أورنج ديما كاينة معاك !🧡\\n\\nمع خدمة solde dépa... \n", "3 لفراجة كاينة مع La Fibre d'Orange !\\nنتا غير ... \n", "4 سخاوة النجمة 3 المضوبلة كاينة ! 😍\\nاستافدو من ... \n", "\n", " title \n", "0 Orange Maroc : خطوة خير \\\n", "1 Orange Maroc: Transfert de solde \n", "2 Orange Maroc: Solde dépannage \n", "3 Orange Maroc : La Fibre d'Orange لفراجة كاينة مع \n", "4 Orange Maroc : النجمة 3 المضوبلة كاينة \n", "\n", " link channel \n", "0 https://www.youtube.com/watch?v=3RoNBU-snrc @orangemaroc \n", "1 https://www.youtube.com/watch?v=oQKRx9i2NdY @orangemaroc \n", "2 https://www.youtube.com/watch?v=ElW8plKHRrI @orangemaroc \n", "3 https://www.youtube.com/watch?v=sDt4tK29Vy8 @orangemaroc \n", "4 https://www.youtube.com/watch?v=U2VOn8mhN8U @orangemaroc " ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_ads.head()" ] }, { "cell_type": "code", "execution_count": 72, "id": "bb4effba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 3992 entries, 0 to 3991\n", "Data columns (total 4 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 ad 3992 non-null object\n", " 1 title 3992 non-null object\n", " 2 link 3992 non-null object\n", " 3 channel 3992 non-null object\n", "dtypes: object(4)\n", "memory usage: 124.9+ KB\n" ] } ], "source": [ "df_ads.info()" ] }, { "cell_type": "code", "execution_count": 70, "id": "76633f3d", "metadata": {}, "outputs": [], "source": [ "# push to huggingface dataset\n", "from datasets import Dataset\n", "\n", "dataset = Dataset.from_pandas(df_ads)" ] }, { "cell_type": "code", "execution_count": 49, "id": "7e41b632", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "40e4f65f82e44cfb8a28f6da043299bd", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value='