{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import re\n", "import time\n", "import requests\n", "\n", "import pandas as pd\n", "import yaml\n", "from tqdm.auto import tqdm\n", "from html_to_markdown import convert_to_markdown\n", "\n", "from selenium import webdriver\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 selenium.webdriver.chrome.options import Options\n", "from selenium.common.exceptions import TimeoutException, NoSuchElementException\n", "\n", "tqdm.pandas()\n", "\n", "from dotenv import load_dotenv\n", "\n", "load_dotenv()\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "DOCS_SITEMAP_XML = \"https://create.roblox.com/docs/sitemap.1.xml\"\n", "LUAU_NAV_YAML = \"https://raw.githubusercontent.com/luau-lang/site/refs/heads/master/_data/navigation.yml\"\n", "\n", "DOCS_CONTENT_MAIN_ID = \"main\" #
\n", "LUAU_CONTENT_DIV_CLASS = \"page__inner-wrap\" #
\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Error sending stats to Plausible: error sending request for url (https://plausible.io/api/event)\n" ] } ], "source": [ "# Setup Selenium WebDriver with Chrome\n", "def create_driver(headless=True):\n", " \"\"\"Create and configure a Chrome WebDriver instance.\"\"\"\n", " chrome_options = Options()\n", " if headless:\n", " chrome_options.add_argument(\"--headless\")\n", " chrome_options.add_argument(\"--no-sandbox\")\n", " chrome_options.add_argument(\"--disable-dev-shm-usage\")\n", " chrome_options.add_argument(\"--disable-gpu\")\n", " chrome_options.add_argument(\"--window-size=1920,1080\")\n", " # Disable images to speed up loading\n", " chrome_options.add_argument(\"--blink-settings=imagesEnabled=false\")\n", "\n", " driver = webdriver.Chrome(options=chrome_options)\n", " return driver\n", "\n", "# Initialize the driver\n", "driver = create_driver(headless=True)\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['https://luau.org/getting-started', 'https://luau.org/why', 'https://luau.org/syntax', 'https://luau.org/lint', 'https://luau.org/performance']\n", "['https://create.roblox.com/docs', 'https://create.roblox.com/docs/id-id/cloud', 'https://create.roblox.com/docs/id-id/education/educator-onboarding/landing', 'https://create.roblox.com/docs/id-id/production/promotion/content-maturity', 'https://create.roblox.com/docs/id-id/production/publishing/about-GDPR-and-CCPA']\n" ] } ], "source": [ "# First, we gather all the URLs we plan to visit using regular requests\n", "luau_nav_response = requests.get(LUAU_NAV_YAML)\n", "luau_nav_response.raise_for_status()\n", "luau_urls = []\n", "for page in yaml.safe_load(luau_nav_response.text).get(\"pages\", []):\n", " luau_urls.append(f\"https://luau.org{page['url']}\")\n", "\n", "docs_sitemap_response = requests.get(DOCS_SITEMAP_XML)\n", "docs_sitemap_response.raise_for_status()\n", "docs_urls = []\n", "for url in re.findall(r'(.*?)', docs_sitemap_response.text, re.DOTALL):\n", " docs_urls.append(url.strip())\n", "\n", "print(luau_urls[:5])\n", "print(docs_urls[:5])\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(\n", " columns=[\n", " \"url\",\n", " \"language\",\n", " \"content\",\n", " ]\n", ")\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "31ff274ace464525a864702aa72cda66", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Processing Luau URLs: 0%| | 0/11 [00:00\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", " \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", "
urllanguagecontent
0https://luau.org/getting-starteden-us\\n# Getting Started\\n\\n#### On this page\\n\\n* ...
1https://luau.org/whyen-us\\n# Why Luau?\\n\\nAround 2006, [Roblox](https:/...
2https://luau.org/syntaxen-us\\n# Syntax\\n\\n#### On this page\\n\\n* [String l...
3https://luau.org/linten-us\\n# Linting\\n\\n#### On this page\\n\\n* [Unknown...
4https://luau.org/performanceen-us\\n# Performance\\n\\n#### On this page\\n\\n* [Fas...
5https://luau.org/sandboxen-us\\n# Sandboxing\\n\\n#### On this page\\n\\n* [Libr...
6https://luau.org/compatibilityen-us\\n# Compatibility\\n\\n#### On this page\\n\\n* [I...
7https://luau.org/typechecken-us\\n# Type checking\\n\\n#### On this page\\n\\n* [T...
8https://luau.org/profileen-us\\n# Profiling\\n\\n#### On this page\\n\\nOne of m...
9https://luau.org/libraryen-us\\n# Library\\n\\n#### On this page\\n\\n* [Global ...
10https://luau.org/grammaren-us\\n# Grammar\\n\\nThis is the complete syntax gra...
\n", "
" ], "text/plain": [ " url language \\\n", "0 https://luau.org/getting-started en-us \n", "1 https://luau.org/why en-us \n", "2 https://luau.org/syntax en-us \n", "3 https://luau.org/lint en-us \n", "4 https://luau.org/performance en-us \n", "5 https://luau.org/sandbox en-us \n", "6 https://luau.org/compatibility en-us \n", "7 https://luau.org/typecheck en-us \n", "8 https://luau.org/profile en-us \n", "9 https://luau.org/library en-us \n", "10 https://luau.org/grammar en-us \n", "\n", " content \n", "0 \\n# Getting Started\\n\\n#### On this page\\n\\n* ... \n", "1 \\n# Why Luau?\\n\\nAround 2006, [Roblox](https:/... \n", "2 \\n# Syntax\\n\\n#### On this page\\n\\n* [String l... \n", "3 \\n# Linting\\n\\n#### On this page\\n\\n* [Unknown... \n", "4 \\n# Performance\\n\\n#### On this page\\n\\n* [Fas... \n", "5 \\n# Sandboxing\\n\\n#### On this page\\n\\n* [Libr... \n", "6 \\n# Compatibility\\n\\n#### On this page\\n\\n* [I... \n", "7 \\n# Type checking\\n\\n#### On this page\\n\\n* [T... \n", "8 \\n# Profiling\\n\\n#### On this page\\n\\nOne of m... \n", "9 \\n# Library\\n\\n#### On this page\\n\\n* [Global ... \n", "10 \\n# Grammar\\n\\nThis is the complete syntax gra... " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "progress = tqdm(luau_urls, desc=\"Processing Luau URLs\")\n", "for url in progress:\n", " progress.set_postfix_str(url)\n", "\n", " try:\n", " # Navigate to the page\n", " driver.get(url)\n", "\n", " # Wait for the content to load\n", " wait = WebDriverWait(driver, 20)\n", " content_element = wait.until(\n", " EC.presence_of_element_located((By.CLASS_NAME, LUAU_CONTENT_DIV_CLASS))\n", " )\n", "\n", " # Additional wait to ensure dynamic content is loaded\n", " time.sleep(0.1)\n", "\n", " # Get the HTML content\n", " content_html = content_element.get_attribute('innerHTML')\n", "\n", " if content_html:\n", " content = convert_to_markdown(content_html, heading_style=\"atx\")\n", " df.loc[len(df)] = [url, \"en-us\", content]\n", " tqdm.write(f\"Processed {url}\")\n", " else:\n", " tqdm.write(f\"Failed to extract content from {url}\")\n", " continue\n", "\n", " except TimeoutException:\n", " tqdm.write(f\"Timeout while loading {url}\")\n", " except NoSuchElementException:\n", " tqdm.write(f\"Could not find content element on {url}\")\n", " except Exception as e:\n", " tqdm.write(f\"Error processing {url}: {e}\")\n", "\n", "progress.close()\n", "df\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ba5df5b98f2c40889a0ed09086fcbf1a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Processing Docs URLs: 0%| | 0/19600 [00:00\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", " \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", "
urllanguagecontent
0https://luau.org/getting-starteden-us\\n# Getting Started\\n\\n#### On this page\\n\\n* ...
1https://luau.org/whyen-us\\n# Why Luau?\\n\\nAround 2006, [Roblox](https:/...
2https://luau.org/syntaxen-us\\n# Syntax\\n\\n#### On this page\\n\\n* [String l...
3https://luau.org/linten-us\\n# Linting\\n\\n#### On this page\\n\\n* [Unknown...
4https://luau.org/performanceen-us\\n# Performance\\n\\n#### On this page\\n\\n* [Fas...
............
19605https://create.roblox.com/docs/ko-kr/education...ko-kr1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/...
19606https://create.roblox.com/docs/ko-kr/education...ko-kr1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/...
19607https://create.roblox.com/docs/ko-kr/education...ko-kr1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/...
19608https://create.roblox.com/docs/ko-kr/education...ko-kr1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/...
19609https://create.roblox.com/docs/ko-kr/creator-p...ko-kr1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/...
\n", "

19610 rows × 3 columns

\n", "" ], "text/plain": [ " url language \\\n", "0 https://luau.org/getting-started en-us \n", "1 https://luau.org/why en-us \n", "2 https://luau.org/syntax en-us \n", "3 https://luau.org/lint en-us \n", "4 https://luau.org/performance en-us \n", "... ... ... \n", "19605 https://create.roblox.com/docs/ko-kr/education... ko-kr \n", "19606 https://create.roblox.com/docs/ko-kr/education... ko-kr \n", "19607 https://create.roblox.com/docs/ko-kr/education... ko-kr \n", "19608 https://create.roblox.com/docs/ko-kr/education... ko-kr \n", "19609 https://create.roblox.com/docs/ko-kr/creator-p... ko-kr \n", "\n", " content \n", "0 \\n# Getting Started\\n\\n#### On this page\\n\\n* ... \n", "1 \\n# Why Luau?\\n\\nAround 2006, [Roblox](https:/... \n", "2 \\n# Syntax\\n\\n#### On this page\\n\\n* [String l... \n", "3 \\n# Linting\\n\\n#### On this page\\n\\n* [Unknown... \n", "4 \\n# Performance\\n\\n#### On this page\\n\\n* [Fas... \n", "... ... \n", "19605 1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/... \n", "19606 1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/... \n", "19607 1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/... \n", "19608 1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/... \n", "19609 1. [생성](/docs/ko-kr/platform)\\n2. /\\n3. [개요](/... \n", "\n", "[19610 rows x 3 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "progress = tqdm(docs_urls, desc=\"Processing Docs URLs\")\n", "for url in progress:\n", " progress.set_postfix_str(url)\n", "\n", " try:\n", " # Navigate to the page\n", " driver.get(url)\n", "\n", " # Wait for the content to load\n", " wait = WebDriverWait(driver, 20)\n", " content_element = wait.until(\n", " EC.presence_of_element_located((By.ID, DOCS_CONTENT_MAIN_ID))\n", " )\n", "\n", " # Additional wait to ensure dynamic content is loaded\n", " time.sleep(0.15)\n", "\n", " # Get the HTML content\n", " content_html = content_element.get_attribute('innerHTML')\n", "\n", " if content_html:\n", " content = convert_to_markdown(content_html, heading_style=\"atx\")\n", "\n", " # Extract language from url\n", " lang_match = re.search(r'/docs/([a-z]{2}-[a-z]{2})/', url)\n", " if lang_match:\n", " language = lang_match.group(1)\n", " else:\n", " language = \"en-us\"\n", "\n", " df.loc[len(df)] = [url, language, content]\n", " tqdm.write(f\"Processed {url}\")\n", " else:\n", " tqdm.write(f\"Failed to extract content from {url}\")\n", " continue\n", "\n", " except TimeoutException:\n", " tqdm.write(f\"Timeout while loading {url}\")\n", " except NoSuchElementException:\n", " tqdm.write(f\"Could not find content element on {url}\")\n", " except Exception as e:\n", " tqdm.write(f\"Error processing {url}: {e}\")\n", "\n", "progress.close()\n", "df\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Processed 19610 pages\n" ] } ], "source": [ "print(f\"Processed {len(df)} pages\")\n", "df.to_parquet(\"train.parquet\")\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Clean up - close the driver\n", "driver.quit()\n" ] } ], "metadata": { "kernelspec": { "display_name": "roblox-info-dump-ptYyoJbN-py3.11", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }