{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c001",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "# קריאת דפי אינטרנט עם requests ו־Beautiful Soup: מאמרי חומרה מ־arXiv\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c002",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**שלושה שיעורים, מחברת אחת.** המחברת הזו עוקבת אחרי שלושה שיעורים של הקורס, זה אחר זה: חלק 1, [מדף אינטרנט לנתונים: Beautiful Soup](https://dvirross.com/he/courses/intro-data-science/data-collection-beautifulsoup) (משימות 1 עד 3); חלק 2, [מאלמנט אחד לכל הרשומות: find_all, מספרים וערכים חסרים](https://dvirross.com/he/courses/intro-data-science/data-collection-find-all) (משימות 4 עד 6); חלק 3, [מרשימה לדפים רבים: עוקבים אחרי קישורים](https://dvirross.com/he/courses/intro-data-science/data-collection-linked-pages) (משימות 7 עד 9). כותרת מסמנת את המקום שבו כל חלק מתחיל. המשימות ממוספרות מ־1 עד 9 בלי הפסקה, ולכן `check(N)` שייכת תמיד למשימה N של החלק שמחזיק אותה.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c003",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "# חלק 1: מדף אינטרנט לנתונים: Beautiful Soup\n",
    "\n",
    "דף השיעור של החלק הזה: [מדף אינטרנט לנתונים: Beautiful Soup](https://dvirross.com/he/courses/intro-data-science/data-collection-beautifulsoup)\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c004",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "בשיעור הזה נלמד איך תוכנית מבקשת דף אינטרנט, איך התגיות של דף [HTML](https://dvirross.com/he/courses/intro-data-science/glossary#html) מסודרות באלמנטים עם תכונות, ואיך הספרייה Beautiful Soup של Python מוצאת אלמנט אחד וקוראת את הטקסט שלו ואת התכונות שלו.\n",
    "\n",
    "## אילו מאמרי חומרה הופיעו החודש?\n",
    "\n",
    "[arXiv](https://arxiv.org) (נהגה ״ארכיב״) הוא ארכיון חופשי שבו חוקרים מפרסמים מאמרים מדעיים. המדור Hardware Architecture, שנקרא `cs.AR`, מפרט מאמרים על תכנון של מעבדים, זיכרונות ומאיצים. הרשימה של אוקטובר 2026 היא דף אינטרנט אחד עם 266 רשומות. אנחנו רוצים את הכותרת, הקישור ומספר המחברים של כל מאמר. הרשימה מוצגת כדף אינטרנט, ובן אדם יכול לקרוא את הנתונים שם.\n",
    "\n",
    "<figure class=\"lesson-shot\">\n",
    "<a href=\"/images/course-illustrations/data-collection/arxiv-listing-entries.webp\"><img src=\"/images/course-illustrations/data-collection/arxiv-listing-entries.webp\" width=\"960\" height=\"400\" alt=\"תחילת הרשימה של מאמרי Hardware Architecture ב־arXiv לאוקטובר 2026: השורה עם מספר הרשומות הכולל, והרשומות הראשונות, ולכל אחת כותרת, מחברים וקישורים למאמר. ארבעה סימנים ממוספרים מצביעים על המספר הכולל, על כותרת, על המחברים ועל הקישורים.\" loading=\"lazy\"></a>\n",
    "<figcaption>הרשימה של אוקטובר 2026, כפי שהדפדפן מציג אותה. סימן 1: מספר הרשומות בחודש. סימן 2: הכותרת של מאמר. סימן 3: המחברים שלו. סימן 4: הקישורים אל ה־pdf שלו, אל גרסת ה־html שלו ואל פורמטים נוספים.</figcaption>\n",
    "</figure>\n",
    "\n",
    "קריאת נתונים מדפי אינטרנט בעזרת תוכנית נקראת [**סריקת אתרים**](https://dvirross.com/he/courses/intro-data-science/glossary#web-scraping) (web scraping). תוכנית יכולה לעשות את מה שהאדם עושה, אם היא יכולה לקרוא את הדף. לספור ביד את המחברים של 266 מאמרים לוקח שעה; לתוכנית זה לוקח שנייה, והיא עושה אותו דבר גם לרשימה של החודש הבא. בשיעור הזה תוכנית מקבלת את הדף וקוראת ממנו את הכותרת ואת הקישור של המאמר הראשון.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c005",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## הכנה: הספריות ופונקציית הבדיקה\n",
    "\n",
    "אין צורך בחשבון ואין צורך במפתח. התא הבא מייבא את `requests` ואת `beautifulsoup4` (ב־Colab יש את שתיהן; התא מתקין אותן אם בסביבה אין). הדפים שהקוד קורא הם עותקים שמורים של דפי arXiv, ששמורים באתר הציבורי של הקורס, ולכן המספרים זהים לכולם. התאים שמסומנים **האתר החי** קוראים את arXiv עצמו, ולכן המספרים בהם עשויים להיות שונים מהמספרים בדף, והם עלולים להיכשל כשהאתר לא עונה. הם כבויים (`RUN_LIVE = False`), כדי שכיתה שלמה לא תשלח בבת אחת בקשות ל־arXiv מאותה כתובת.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c006",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.769466Z",
     "iopub.status.busy": "2026-11-22T13:17:24.769289Z",
     "iopub.status.idle": "2026-11-22T13:17:24.808976Z",
     "shell.execute_reply": "2026-11-22T13:17:24.808008Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "requests 2.34.2 | beautifulsoup4 4.15.0\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    import bs4, requests\n",
    "except ImportError:\n",
    "    !pip install -q requests beautifulsoup4\n",
    "    import bs4, requests\n",
    "print('requests', requests.__version__, '| beautifulsoup4', bs4.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c007",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "התא הבא מגדיר את הפונקציה `check(task_number)`. אחרי שמריצים את התא של משימה, מריצים את התא שמתחתיו: `check` מריצה שוב את התא שלכם, אוספת את מה שהוא מדפיס ומשווה אותו לפלט הצפוי. היא אומרת אם הפלט נכון, ואם לא באיזו שורה הוא שונה; היא לא מראה את הפתרון.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c008",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.811720Z",
     "iopub.status.busy": "2026-11-22T13:17:24.811436Z",
     "iopub.status.idle": "2026-11-22T13:17:24.820655Z",
     "shell.execute_reply": "2026-11-22T13:17:24.819559Z"
    }
   },
   "outputs": [],
   "source": [
    "import ast, contextlib, hashlib, io\n",
    "\n",
    "# The expected output of each task, as hashes of its lines (the solutions are not stored here).\n",
    "EXPECTED = {int(k): v for k, v in {\n",
    "    '1': [\n",
    "        'a4ca3edcead4'\n",
    "    ],\n",
    "    '2': [\n",
    "        '2dfe249ea3aa'\n",
    "    ],\n",
    "    '3': [\n",
    "        'c9ad4f15cacd'\n",
    "    ],\n",
    "    '4': [\n",
    "        'cacbc3d3f1c4'\n",
    "    ],\n",
    "    '5': [\n",
    "        '19581e27de7c'\n",
    "    ],\n",
    "    '6': [\n",
    "        '7b1bd53949b7'\n",
    "    ],\n",
    "    '7': [\n",
    "        '4e07408562be'\n",
    "    ],\n",
    "    '8': [\n",
    "        'ead42beead86'\n",
    "    ],\n",
    "    '9': [\n",
    "        '4652614c4d87'\n",
    "    ]\n",
    "}.items()}\n",
    "\n",
    "\n",
    "def check(task):\n",
    "    \"\"\"Run the last cell that ran (not a check) again and compare what it prints with the expected output of the task.\"\"\"\n",
    "    if task not in EXPECTED:\n",
    "        print('אין משימה כזו: ', task)\n",
    "        return\n",
    "    cells = [s for s in In[:-1] if s.strip() and not s.lstrip().startswith('check(')]   # In: the cells that ran, kept by IPython\n",
    "    tree = ast.parse(cells[-1] if cells else '')\n",
    "    last = tree.body.pop() if tree.body and isinstance(tree.body[-1], ast.Expr) else None   # a bare last expression counts as printed\n",
    "    out = io.StringIO()\n",
    "    try:\n",
    "        with contextlib.redirect_stdout(out):\n",
    "            exec(compile(tree, '<task>', 'exec'), globals())\n",
    "            if last is not None:\n",
    "                value = eval(compile(ast.Expression(last.value), '<task>', 'eval'), globals())\n",
    "                if value is not None:\n",
    "                    print(repr(value))\n",
    "    except Exception as error:\n",
    "        print('התא נכשל בשגיאה:', type(error).__name__ + ':', error)\n",
    "        return\n",
    "    got = [hashlib.sha256(line.rstrip().encode()).hexdigest()[:12] for line in out.getvalue().rstrip().splitlines()]\n",
    "    want = EXPECTED[task]\n",
    "    if not got:\n",
    "        print('לא הודפס כלום. הדפיסו את התשובה בתא המשימה, הריצו אותו, והריצו את `check` מיד אחריו.')\n",
    "    elif len(got) != len(want):\n",
    "        print('התא הדפיס {got} שורות והמשימה מצפה ל־{want}. קראו שוב את המשימה ואת הרמז.'.format(got=len(got), want=len(want)))\n",
    "    elif got != want:\n",
    "        print('שורה {k} שונה מהפלט הצפוי. קראו שוב את המשימה ואת הרמז.'.format(k=next(i for i in range(len(want)) if got[i] != want[i]) + 1))\n",
    "    else:\n",
    "        print('OK: הפלט כצפוי.')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c009",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## אתר התרגול והעותק השמור שלו\n",
    "\n",
    "הדפים של היחידה הזו הם דפים אמיתיים של arXiv, וה־[`robots.txt`](https://dvirross.com/he/courses/intro-data-science/glossary#robots-txt) שלו (קובץ שבו אתר מספר לתוכנות אילו מדפיו מותר להן לקרוא) מתיר את הדפים שבהם נשתמש. הדפדפן מתיר לדף באתר הזה לפנות רק לאתר הזה, ולכן הקוד בדף הזה קורא **עותק שמור** של הדפים, ששמור באתר הזה: הרשימה של אוקטובר 2026 (25 הרשומות הראשונות שלה) והדף של כל אחד משמונת המאמרים הראשונים שלה. העותקים הם הדפים כפי ש־arXiv שלח אותם ב־13 בנובמבר 2026, בלי שינוי. המחברות של היחידה מריצות את אותו קוד על העותק; התאים שלהן לאתר החי, שבו המספרים עשויים להיות שונים, כבויים (`RUN_LIVE = False`): הכיתה חולקת כתובת אחת, ו־arXiv מבקש 15 שניות בין בקשות. הדפים השמורים מכילים מידע תיאורי על המאמרים (כותרות, מחברים, תקצירים ונושאים), ש־arXiv מתיר לשימוש חופשי, לפי [תנאי השימוש של ה־API שלו](https://info.arxiv.org/help/api/tou.html) (הקדשה לנחלת הכלל, CC0 1.0); המאמרים עצמם שייכים למחבריהם ולא הועתקו לכאן. המקור של כל דף הוא [arXiv.org](https://arxiv.org). הקישורים שבתוך עותק מובילים אל arXiv עצמו, ולכן הם לא נפתחים מהאתר הזה.\n",
    "\n",
    "## ארבעה כללים לפני שקוראים אתר\n",
    "\n",
    "תוכנית שקוראת אתר היא אורחת. ארבעה כללים עוזרים לה להישאר אורחת רצויה:\n",
    "\n",
    "- נקרא את [`robots.txt`](https://dvirross.com/he/courses/intro-data-science/glossary#robots-txt) של האתר ונפעל לפיו: הוא אומר אילו דפים מותר לתוכניות לקרוא.\n",
    "- נמתין בין בקשה לבקשה: arXiv מבקש 15 שניות, וכיתה שחולקת כתובת אחת נחשבת למבקר אחד.\n",
    "- נציג את עצמנו: בקשה יכולה לשאת שם ודרך ליצור איתנו קשר (הסעיף [לקרוא דפים באחריות](#לקרוא-דפים-באחריות) מראה איך).\n",
    "- נקח רק את מה שהמשימה צריכה, ובלי מידע אישי.\n",
    "\n",
    "## דף הוא טקסט עם תגיות\n",
    "\n",
    "כשמקלידים כתובת בדפדפן, הדפדפן שולח **בקשה** (request) אל ה**שרת** שמחזיק את הדף, והשרת שולח בחזרה **תשובה** (response). ההחלפה הזו נעשית לפי הכללים של [HTTP](https://dvirross.com/he/courses/intro-data-science/glossary#http), הדרך שבה האינטרנט מדבר, והכתובת היא [URL](https://dvirross.com/he/courses/intro-data-science/glossary#url). התשובה מחזיקה את הטקסט של הדף, כתוב ב־[HTML](https://dvirross.com/he/courses/intro-data-science/glossary#html): טקסט שעטוף ב**תגיות** (tags), מילים בסוגריים זוויתיים כמו `<dt>` ו־`</dt>`, שאומרות לדפדפן מה כל חלק: כותרת, קישור, רשימה. הדפדפן מסתיר את התגיות ומצייר את הדף. תוכנית שמבקשת את הדף מקבלת את התגיות גם כן.\n",
    "\n",
    "הדף בנוי מ**אלמנטים** (elements). אלמנט הוא חלק של הדף שבין תגית פותחת לתגית סוגרת, והוא יכול להחזיק טקסט ואלמנטים אחרים. הנה תחילת הרשומה הראשונה ברשימה, כפי שהיא בטקסט של הדף, עם חלקים שהושמטו:\n",
    "\n",
    "```html\n",
    "<dt><a name='item1'>[1]</a>\n",
    "  <a href=\"/abs/2610.00106\" title=\"Abstract\">arXiv:2610.00106</a>\n",
    "<dd>\n",
    "  <div class='meta'>\n",
    "    <div class='list-title mathjax'><span class='descriptor'>Title:</span>\n",
    "      SyntheticHLS: Building Diverse Synthetic High-Level ...\n",
    "    </div>\n",
    "    <div class='list-authors'>\n",
    "      <a href=\"https://arxiv.org/search/...\">Stefan Abi-Karam</a>, ...\n",
    "    </div>\n",
    "```\n",
    "\n",
    "- **קינון.** האלמנט `dd` מחזיק את ה־`div` מהסוג `meta`, שמחזיק את ה־`div` של הכותרת, שמחזיק `span`. האלמנטים הפנימיים הם ה**ילדים** (children) של החיצוני.\n",
    "- **תכונות.** תגית פותחת יכולה לשאת תכונות (attributes), זוגות של שם וערך כמו `href=\"/abs/2610.00106\"` או `title=\"Abstract\"`. הטקסט של אלמנט הוא מה שהקורא רואה; תכונה היא מידע על האלמנט, למשל לאן קישור מוביל.\n",
    "- **סוג.** התכונה `class` נותנת לאלמנט שם אחד או יותר, כאן `list-title` ו־`mathjax`. אלמנטים מאותו סוג חולקים שם, ולכן ה־`class` הוא הדרך של תוכנית להבדיל בין כותרת לשורת מחברים.\n",
    "\n",
    "הדפדפן מראה את האלמנטים של הדף כעץ. לחיצה ימנית על חלק של הדף ובחירה ב**בדיקה** (Inspect) פותחות את כלי המפתחים, והאלמנט שלחצנו עליו מסומן. כשרוצים נתונים מדף, לפי המבט הזה נחליט מה לכתוב בקוד: באיזו תגית הם נמצאים, איזה `class` או איזו תכונה מבדילים אותם מהאלמנטים שסביבם, והאם צריך את הטקסט שלהם או תכונה.\n",
    "\n",
    "<figure class=\"lesson-shot\">\n",
    "<a href=\"/images/course-illustrations/data-collection/arxiv-devtools-entry.webp\"><img src=\"/images/course-illustrations/data-collection/arxiv-devtools-entry.webp\" width=\"773\" height=\"462\" alt=\"כלי המפתחים של דפדפן על הרשימה של arXiv, עם עץ האלמנטים פתוח על הרשומה הראשונה. ארבעה סימנים ממוספרים מדגישים את התגית הפותחת של אלמנט, את התכונה class שלו, את הטקסט שלו ואת התכונה href של קישור.\" loading=\"lazy\"></a>\n",
    "<figcaption>עץ האלמנטים של הרשומה הראשונה. סימן 1: תגית, השם של האלמנט. סימן 2: התכונה <code>class</code>, שאומרת מאיזה סוג האלמנט. סימן 3: הטקסט שהאלמנט מחזיק, הכותרת של המאמר. סימן 4: התכונה <code>href</code> של קישור, הכתובת שאליה הוא מוביל.</figcaption>\n",
    "</figure>\n",
    "\n",
    "## מבקשים את הדף מ־Python\n",
    "\n",
    "הספרייה `requests` שולחת בקשה מ־Python. השורה הראשונה בתיבה הבאה מייבאת אותה. הפונקציה `requests.get` שולחת בקשה שמבקשת דף, ומחזירה **אובייקט תשובה** (response object): כל מה שחזר, שמור במשתנה אחד. קודם נקרא ממנו שני דברים: `status_code`, מספר שאומר איך הבקשה עברה (200 אומר שהדף הגיע), ו־`text`, הטקסט של הדף כמחרוזת אחת ארוכה. הפונקציה `len` מחזירה את מספר התווים של מחרוזת. הכתובת ארוכה, ולכן היא כתובה בשני חלקים: שתי מחרוזות צמודות במירכאות, בתוך סוגריים, מחוברות ב־Python למחרוזת אחת, והסוגריים מאפשרים להן להמשיך בשורה הבאה.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c010",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.824432Z",
     "iopub.status.busy": "2026-11-22T13:17:24.824192Z",
     "iopub.status.idle": "2026-11-22T13:17:24.830721Z",
     "shell.execute_reply": "2026-11-22T13:17:24.829696Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "46598\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "list_url = (\n",
    "    'https://dvirross.com/materials/courses/'\n",
    "    'intro-data-science/arxiv-cs-ar-2026-10.htm'\n",
    ")\n",
    "response = requests.get(list_url)\n",
    "print(response.status_code)\n",
    "html = response.text\n",
    "print(len(html))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c011",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "אורך הדף הוא 46,598 תווים, הרבה יותר ממה שרואים על המסך, כי רובו תגיות.\n",
    "\n",
    "## Beautiful Soup קוראת את התגיות\n",
    "\n",
    "**Beautiful Soup** היא ספרייה של Python שקוראת את הטקסט של דף HTML ובונה ממנו את העץ של האלמנטים שלו, כדי שנוכל לבקש אלמנט במקום לחתוך טקסט. תוכנית שקוראת טקסט בפורמט נתון ובונה ממנו מבנה היא [**מנתח**](https://dvirross.com/he/courses/intro-data-science/glossary#parser) (parser). הספרייה `beautifulsoup4` מיובאת בשם `bs4`, והמחלקה שבה נשתמש היא `BeautifulSoup`. היא מקבלת שני ארגומנטים: את הטקסט של הדף, ואת השם של המנתח, `'html.parser'`, זה שמגיע עם Python.\n",
    "\n",
    "<p><small>השם לקוח מהשיר ״Beautiful Soup״ שהצב המדומה שר בספר ״הרפתקאות עליסה בארץ הפלאות״ מאת לואיס קרול. הוא גם משחק מילים על ״tag soup״ (מרק תגיות), הכינוי ל־HTML מבולגן ובנוי גרוע, מהסוג שהספרייה נועדה לקרוא.</small></p>\n",
    "\n",
    "נכיר עוד שני שמות. `soup.title` הוא האלמנט הראשון מהתגית `title` בדף, והשם `text` שנכתב אחרי אלמנט, כמו ב־`soup.title.text`, נותן את כל הטקסט שבתוכו.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c012",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.832393Z",
     "iopub.status.busy": "2026-11-22T13:17:24.832173Z",
     "iopub.status.idle": "2026-11-22T13:17:24.870847Z",
     "shell.execute_reply": "2026-11-22T13:17:24.869639Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<title>Hardware Architecture Oct 2026</title>\n",
      "Hardware Architecture Oct 2026\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "\n",
    "soup = BeautifulSoup(html, 'html.parser')\n",
    "print(soup.title)\n",
    "print(soup.title.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c013",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "השורה הראשונה הדפיסה את האלמנט עם התגיות שלו, והשנייה רק את הטקסט שהוא מחזיק.\n",
    "\n",
    "## `find`: האלמנט הראשון שמתאים\n",
    "\n",
    "השיטה `find` של ה־`soup` מחזירה את האלמנט הראשון שמתאים למה שביקשנו. נעביר לה שם של תגית, ובנוסף, כדי להבדיל בין אלמנטים מאותה תגית, `class`. המילה `class` שמורה ב־Python, ולכן הארגומנט נכתב `class_`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c014",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.872777Z",
     "iopub.status.busy": "2026-11-22T13:17:24.872591Z",
     "iopub.status.idle": "2026-11-22T13:17:24.876919Z",
     "shell.execute_reply": "2026-11-22T13:17:24.875266Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title:\n",
      "          SyntheticHLS: Building Diverse Synthetic High-Level Synthesis Datasets using LLMs\n",
      "        \n"
     ]
    }
   ],
   "source": [
    "title = soup.find('div', class_='list-title')\n",
    "print(title.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c015",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "הטקסט של האלמנט הוא מה שהכותרת מחזיקה, עם האלמנט הפנימי שלה: המילה `Title:` של ה־`span`, והרווחים ומעברי השורה של הדף סביב הכותרת. שתי שיטות של מחרוזת מנקות אותו. `replace('Title:', '')` מחליפה חלק מהטקסט בטקסט אחר, כאן בכלום, ו־`strip()` מסירה את הרווחים ואת מעברי השורה משני הקצוות של המחרוזת.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c016",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.879125Z",
     "iopub.status.busy": "2026-11-22T13:17:24.878576Z",
     "iopub.status.idle": "2026-11-22T13:17:24.882620Z",
     "shell.execute_reply": "2026-11-22T13:17:24.881495Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SyntheticHLS: Building Diverse Synthetic High-Level Synthesis Datasets using LLMs\n"
     ]
    }
   ],
   "source": [
    "print(title.text.replace('Title:', '').strip())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c017",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**נסו בעצמכם.** מה `find` מחזירה כשאף אלמנט לא מתאים? נבאו מה התיבה מדפיסה, ואז הריצו אותה. בדף הזה אין אלמנט מהתגית `table`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c018",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.885195Z",
     "iopub.status.busy": "2026-11-22T13:17:24.884990Z",
     "iopub.status.idle": "2026-11-22T13:17:24.888979Z",
     "shell.execute_reply": "2026-11-22T13:17:24.888025Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(soup.find('table'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c019",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## תכונה נקראת עם `get`\n",
    "\n",
    "הקישור לדף של המאמר הראשון אינו טקסט על המסך: הוא התכונה `href` של אלמנט `a`. השיטה `get` של אלמנט מחזירה את הערך של תכונה אחת, לפי שמה. כדי למצוא את האלמנט, `find` מקבלת כל תכונה כארגומנט: האלמנט `a` שהתכונה `title` שלו היא `Abstract`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c020",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.891521Z",
     "iopub.status.busy": "2026-11-22T13:17:24.891327Z",
     "iopub.status.idle": "2026-11-22T13:17:24.895378Z",
     "shell.execute_reply": "2026-11-22T13:17:24.894022Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arXiv:2610.00106\n",
      "/abs/2610.00106\n"
     ]
    }
   ],
   "source": [
    "link = soup.find('a', title='Abstract')\n",
    "print(link.text.strip())\n",
    "print(link.get('href'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c021",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "הטקסט של הקישור הוא מה שהקורא רואה, המזהה של המאמר; התכונה `href` שלו היא לאן הקישור מוביל. הכתובת מתחילה בלוכסן ואין בה שם אתר: היא יחסית לאתר של הדף, שהוא `https://arxiv.org`, ולכן הדף של המאמר הזה הוא `https://arxiv.org/abs/2610.00106`.\n",
    "\n",
    "עד כאן נתנו את התכונה כארגומנט, כמו `title='Abstract'` שמתאים ל־`<a title=\"Abstract\">`. זה עובד כל עוד שם התכונה אינו שם של פרמטר של `find`. בראש הרשומה הראשונה יש את האלמנט `<a name=\"item1\">[1]</a>`, והתכונה שלו נקראת `name`. הפרמטר הראשון של `find` נקרא גם הוא `name`, והוא שם התגית, ולכן `find('a', name='item1')` מעלה שגיאת `TypeError`. הארגומנט `attrs` כותב את התנאי על התכונה כמילון, ועובד לכל תכונה. נשתמש בו כשהתכונה נקראת `name`, או כששמה אינו חוקי כארגומנט, כמו `data-id` (ל־`class` יש גם הצורה `class_`). את העוגן `item1` נמצא כך:\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c022",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.896929Z",
     "iopub.status.busy": "2026-11-22T13:17:24.896738Z",
     "iopub.status.idle": "2026-11-22T13:17:24.900381Z",
     "shell.execute_reply": "2026-11-22T13:17:24.899429Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "anchor = soup.find('a', attrs={'name': 'item1'})\n",
    "print(anchor.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c023",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## מתקדמים בתוך אלמנט\n",
    "\n",
    "לאלמנט ש־`find` החזירה יש `find` משלו, שמחפשת רק בתוכו. כך נצמצם את החיפוש: קודם הרשומה, ואחר כך החלק של הרשומה. האלמנט `dt` הראשון בדף הוא השורה עם המזהה של המאמר. הקישור הראשון שבו, `dt.a`, הוא קישור שאין לו `href`: נראה מה `get` מחזירה.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c024",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.902064Z",
     "iopub.status.busy": "2026-11-22T13:17:24.901893Z",
     "iopub.status.idle": "2026-11-22T13:17:24.906456Z",
     "shell.execute_reply": "2026-11-22T13:17:24.904973Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a name=\"item1\">[1]</a>\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "entry = soup.find('dt')\n",
    "print(entry.a)\n",
    "print(entry.a.get('href'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c025",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "ה־`a` הראשון ברשומה הוא העוגן `[1]`, עם התכונה `name`, שהדף משתמש בה לקפיצות משלו. אין לו `href`, ולכן `get` מחזירה `None` ולא נכשלת. ברשומה יש עוד קישורים, והקישור שאנחנו רוצים הוא זה עם `title='Abstract'`. קריאה לפי מקום, ״הקישור הראשון״, היא הנקודה החלשה של תוכנית; קריאה לפי מה שהאלמנט הוא, ה־`class` שלו או התכונה שלו, היא הנקודה החזקה.\n",
    "\n",
    "אפשר להגיע לאלמנטים שבתוך אלמנט גם לפי שם התגית, רמה אחרי רמה, עם נקודות: `first.div` הוא ה־`div` הראשון שבתוך הרשומה `first`, ו־`.div` שוב הוא ה־`div` הראשון שבתוכו, שהוא כאן הכותרת. השיטה `get_text` מחזירה את הטקסט של אלמנט כמו `text`, ומקבלת ארגומנטים: `get_text(' ', strip=True)` מחברת את חלקי הטקסט ברווח ומסירה את הרווחים משני הקצוות.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c026",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.909591Z",
     "iopub.status.busy": "2026-11-22T13:17:24.909380Z",
     "iopub.status.idle": "2026-11-22T13:17:24.915348Z",
     "shell.execute_reply": "2026-11-22T13:17:24.912258Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: SyntheticHLS: Building Diverse Synthetic High-Level Synthesis Datasets using LLMs\n"
     ]
    }
   ],
   "source": [
    "first = soup.find('dd')\n",
    "print(first.div.div.get_text(' ', strip=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c027",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "הנתיב `first.div.div` תלוי במקום של כל אלמנט, ולכן הוא נשבר כשהדף מוסיף `div` לפני הכותרת. קריאה לפי `class_` לא נשברת.\n",
    "\n",
    "### תרגול\n",
    "\n",
    "**נסו בעצמכם.** הדפיסו את הכותרת של המאמר הראשון ברשימה, בלי המילה `Title:` ובלי הרווחים סביבה, בשורה אחת.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c028",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.917032Z",
     "iopub.status.busy": "2026-11-22T13:17:24.916856Z",
     "iopub.status.idle": "2026-11-22T13:17:24.919844Z",
     "shell.execute_reply": "2026-11-22T13:17:24.918728Z"
    }
   },
   "outputs": [],
   "source": [
    "# Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c029",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**פלט צפוי (המספרים מוסתרים)**\n",
    "\n",
    "```text\n",
    "SyntheticHLS: Building Diverse Synthetic High-Level Synthesis Datasets using LLMs\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c030",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.921486Z",
     "iopub.status.busy": "2026-11-22T13:17:24.921319Z",
     "iopub.status.idle": "2026-11-22T13:17:24.925248Z",
     "shell.execute_reply": "2026-11-22T13:17:24.923898Z"
    }
   },
   "outputs": [],
   "source": [
    "check(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c031",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **רמז**\n",
    ">\n",
    "> הכותרת נמצאת ב־`div` מהסוג `list-title`. קחו את ה־`text` שלו, הסירו את `'Title:'` עם `replace`, ונקו את הקצוות עם `strip`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c032",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**נסו בעצמכם.** ה־pdf של המאמר הראשון הוא קישור שהתכונה `title` שלו שווה ל־`Download PDF`. הדפיסו את הכתובת שבתכונה `href` שלו.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c033",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.927821Z",
     "iopub.status.busy": "2026-11-22T13:17:24.927631Z",
     "iopub.status.idle": "2026-11-22T13:17:24.930784Z",
     "shell.execute_reply": "2026-11-22T13:17:24.929719Z"
    }
   },
   "outputs": [],
   "source": [
    "# Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c034",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**פלט צפוי (המספרים מוסתרים)**\n",
    "\n",
    "```text\n",
    "/pdf/<number>.<number>\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c035",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.932544Z",
     "iopub.status.busy": "2026-11-22T13:17:24.932389Z",
     "iopub.status.idle": "2026-11-22T13:17:24.935837Z",
     "shell.execute_reply": "2026-11-22T13:17:24.934710Z"
    }
   },
   "outputs": [],
   "source": [
    "check(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c036",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **רמז**\n",
    ">\n",
    "> כמו בקישור אל התקציר, `find('a', title='Download PDF')` מחזירה את האלמנט, ו־`get('href')` מחזירה את הכתובת.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c037",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**נסו בעצמכם.** הנושא של מאמר נמצא ב־`span` מהסוג `primary-subject`. הדפיסו את הטקסט של האלמנט הזה למאמר הראשון ברשימה.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c038",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.937834Z",
     "iopub.status.busy": "2026-11-22T13:17:24.937669Z",
     "iopub.status.idle": "2026-11-22T13:17:24.940596Z",
     "shell.execute_reply": "2026-11-22T13:17:24.939516Z"
    }
   },
   "outputs": [],
   "source": [
    "# Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c039",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**פלט צפוי (המספרים מוסתרים)**\n",
    "\n",
    "```text\n",
    "Hardware Architecture (cs.AR)\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c040",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.942041Z",
     "iopub.status.busy": "2026-11-22T13:17:24.941890Z",
     "iopub.status.idle": "2026-11-22T13:17:24.945182Z",
     "shell.execute_reply": "2026-11-22T13:17:24.944118Z"
    }
   },
   "outputs": [],
   "source": [
    "check(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c041",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **רמז**\n",
    ">\n",
    "> `find('span', class_='primary-subject')` מחזירה את האלמנט הראשון בדף שמתאים, והוא של המאמר הראשון.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c042",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## נקודות שכדאי לזכור\n",
    "\n",
    "- לאלמנט יש תגית, תכונות ותוכן, והוא יכול להחזיק אלמנטים אחרים. התכונה `class` אומרת מאיזה סוג האלמנט; התכונה `href` של קישור היא לאן הוא מוביל.\n",
    "- `BeautifulSoup(html, 'html.parser')` בונה את עץ האלמנטים מהטקסט של הדף.\n",
    "- `soup.find(tag, class_=name)` מחזירה את האלמנט הראשון שמתאים, או `None`; `find` מקבלת גם כל תכונה כארגומנט (`title='Abstract'`) או מילון `attrs`.\n",
    "- `text` של אלמנט הוא הטקסט שלו, `get('name')` היא הערך של תכונה, ו־`get` מחזירה `None` כשהתכונה חסרה.\n",
    "- `replace` ו־`strip` מנקות טקסט; לאלמנט יש `find` משלו שמחפשת רק בתוכו.\n",
    "- נקרא לפי מה שהאלמנט הוא (`class`, תכונה), ולא לפי המקום שלו בדף.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c043",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**האתר החי.** התא שלמטה יכול לבקש מ־arXiv עצמו את 25 הרשומות הראשונות של אוקטובר 2026, עם סוכן משתמש שאומר איך אפשר ליצור איתנו קשר. הוא כבוי (`RUN_LIVE = False`): כיתה שלמה שמריצה אותו יחד שולחת הרבה בקשות מאותה כתובת, ו־arXiv מבקש מתוכנות להמתין 15 שניות בין בקשות. אדם אחד, פעם אחת, יכול לשנות ל־`RUN_LIVE = True` כדי לראות את האתר החי. שאר המחברת עובדת על העותק השמור.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c044",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.946790Z",
     "iopub.status.busy": "2026-11-22T13:17:24.946611Z",
     "iopub.status.idle": "2026-11-22T13:17:24.949892Z",
     "shell.execute_reply": "2026-11-22T13:17:24.948998Z"
    }
   },
   "outputs": [],
   "source": [
    "RUN_LIVE = False  # switch to True to ask arXiv itself (one request)\n",
    "if RUN_LIVE:\n",
    "    live_headers = {'User-Agent': 'IntroDataScienceCourse/1.0 (teaching example; contact via dvirross.com)'}\n",
    "    live = requests.get('https://arxiv.org/list/cs.AR/2026-10?skip=0&show=25', headers=live_headers, timeout=30)\n",
    "    if live.status_code in (403, 429):\n",
    "        print('The site refused the request or asked to slow down (status', live.status_code, '). Do not try again now; wait and use the saved copy.')\n",
    "    else:\n",
    "        print(live.status_code, len(live.text), live.text.count('<dt>'))\n",
    "else:\n",
    "    print('The live cell is off. Set RUN_LIVE = True to run it.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c045",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **ללימוד עצמי, חשיבות 1.** קטע ללימוד עצמי: הוא אינו נלמד בהרצאה, ונקרא אותו בעצמנו. קריאת דפים באחריות. הקטע מסייע בנושא: קריאת אתר באחריות (robots.txt, קצב בקשות, מי אנחנו).\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c046",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## לקרוא דפים באחריות\n",
    "\n",
    "תוכנית שקוראת אתר היא אורחת. כמה כללים עוזרים לה להישאר אורחת רצויה.\n",
    "\n",
    "- **`robots.txt`.** כל אתר רשאי לפרסם קובץ בשם הזה בכתובת העליונה שלו, שבו הוא מבקש מתוכנות אילו דפים לא לקרוא ובאיזה קצב לקרוא את האחרים. זו בקשה שתוכנות זהירות מכבדות, לא מנעול. עותק שמור של הקובץ של arXiv נמצא באתר הזה, והתיבה שלמטה מדפיסה את השורות הראשונות של הקבוצה הראשונה שבו: `split('User-agent:')` חותכת את הטקסט בכל קבוצה, ו־`splitlines()` מחזירה את הטקסט של קבוצה כרשימה של שורות.\n",
    "- **קצב.** השורה `Crawl-delay: 15` מבקשת 15 שניות בין שתי בקשות. תוכנית ששולחת בקשות בזו אחר זו בלי הפסקה עלולה להאט את האתר לקוראים שלו, ותיחסם. בקוד ההפסקה היא שורה אחת, `time.sleep(15)`, שעוצרת את התוכנית ל־15 שניות.\n",
    "- **מי מבקש.** כל בקשה נושאת את השם של התוכנית ששולחת אותה, [סוכן המשתמש](https://dvirross.com/he/courses/intro-data-science/glossary#user-agent) (user agent). הספרייה `requests` שולחת את השם `python-requests` אלא אם משנים אותו, ואתרים מסוימים מסרבים לשם הזה: ב־13 בנובמבר 2026 ויקיפדיה האנגלית ענתה לו בקוד המצב 403. תוכנית מנומסת שולחת שם משלה, שאומר מה היא ואיך אפשר להגיע לבעלים שלה, בארגומנט `headers` של `requests.get`.\n",
    "- **תנאי שימוש וזכויות.** המאמרים שייכים למחבריהם, ותנאי השימוש של אתר אומרים מה מותר להעתיק ואיפה מותר להציג. העותק של הדפים באתר הזה נועד ללימוד, והוא אינו עותק לאתר אחר.\n",
    "- **נתונים אישיים.** הדף של מאמר מראה את השם של מי שהגיש אותו. השאלות של השיעורים האלה לא צריכות אותו, ולכן התוכניות לא קוראות אותו. נאסוף רק מה שהשאלה צריכה, ונשקול היטב לפני שנבנה טבלה של אנשים.\n",
    "- **דלת טובה יותר.** כשאתר מציע את הנתונים שלו דרך API, הממשק שנבנה לתוכנות, זו הדרך המנומסת לבקש הרבה נתונים. arXiv מתעד ממשק כזה לאוספים גדולים.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c047",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.951595Z",
     "iopub.status.busy": "2026-11-22T13:17:24.951428Z",
     "iopub.status.idle": "2026-11-22T13:17:24.956464Z",
     "shell.execute_reply": "2026-11-22T13:17:24.955389Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "Crawl-delay: 15\n",
      "Allow: /archive\n",
      "Allow: /year\n",
      "Allow: /list\n"
     ]
    }
   ],
   "source": [
    "robots_url = (\n",
    "    'https://dvirross.com/materials/courses/'\n",
    "    'intro-data-science/arxiv-robots.txt'\n",
    ")\n",
    "robots = requests.get(robots_url).text\n",
    "first_group = robots.split('User-agent:')[1]\n",
    "for line in first_group.splitlines()[:5]:\n",
    "    print(line.strip())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c048",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "בקשה לאתר החי, עם סוכן משתמש משלה וההפסקה בין הבקשות, נראית כך:\n",
    "\n",
    "```python\n",
    "import time\n",
    "\n",
    "headers = {'User-Agent': 'IntroDataScienceCourse/1.0 (teaching example; contact via dvirross.com)'}\n",
    "for address in addresses:\n",
    "    page = requests.get(address, headers=headers)\n",
    "    time.sleep(15)\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c049",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **ללימוד עצמי, חשיבות 2.** קטע ללימוד עצמי: הוא אינו נלמד בהרצאה, ונקרא אותו בעצמנו. קודי מצב וכותרות. הקטע מסייע בנושא: בדיקה שבקשה הצליחה.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c050",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## קודי מצב וכותרות\n",
    "\n",
    "שרת עונה לכל בקשה ב[**קוד מצב**](https://dvirross.com/he/courses/intro-data-science/glossary#status-code) (status code), ותוכנית בודקת אותו לפני שהיא משתמשת בטקסט. בקשה לדף שלא קיים לא נכשלת ב־Python: היא מחזירה תשובה שהקוד שלה הוא 404.\n",
    "\n",
    "| קוד | משמעות |\n",
    "| --- | --- |\n",
    "| 200 | הדף הגיע |\n",
    "| 403 | השרת מסרב לבקשה הזו |\n",
    "| 404 | אין דף כזה |\n",
    "| 429 | יותר מדי בקשות: להמתין |\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c051",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.958097Z",
     "iopub.status.busy": "2026-11-22T13:17:24.957931Z",
     "iopub.status.idle": "2026-11-22T13:17:24.962258Z",
     "shell.execute_reply": "2026-11-22T13:17:24.961299Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "404\n",
      "text/html; charset=utf-8\n"
     ]
    }
   ],
   "source": [
    "missing_url = (\n",
    "    'https://dvirross.com/materials/courses/'\n",
    "    'intro-data-science/arxiv-no-such-page.htm'\n",
    ")\n",
    "missing = requests.get(missing_url)\n",
    "print(missing.status_code)\n",
    "print(response.headers['Content-Type'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c052",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "לתשובה יש גם `headers`, מילון עם מידע על התשובה. `Content-Type` אומר מה סוג התוכן, ובאיזה קידוד טקסט השרת השתמש.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c053",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **ללימוד עצמי, חשיבות 3.** קטע ללימוד עצמי: הוא אינו נלמד בהרצאה, ונקרא אותו בעצמנו. טקסט וקידוד. הקטע מסייע בנושא: טקסט עם אותיות שאינן באלפבית האנגלי.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c054",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## טקסט ובתים: הקידוד\n",
    "\n",
    "התשובה מחזיקה את הדף פעמיים: `text` היא מחרוזת של תווים, ו־`content` היא הבתים שהשרת שלח. תווים שאינם באלפבית האנגלי, כמו אות עם סימן במחבר של מאמר, תופסים שני בתים או יותר, ולכן הבתים רבים מהתווים. הספרייה הופכת את הבתים לטקסט בקידוד שהשרת הצהיר עליו ב־`Content-Type`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c055",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.964107Z",
     "iopub.status.busy": "2026-11-22T13:17:24.963933Z",
     "iopub.status.idle": "2026-11-22T13:17:24.967634Z",
     "shell.execute_reply": "2026-11-22T13:17:24.966485Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "46598\n",
      "46606\n"
     ]
    }
   ],
   "source": [
    "print(len(response.text))\n",
    "print(len(response.content))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c056",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "כששרת לא מצהיר על הקידוד, הטקסט של תו כזה עלול לצאת שגוי. אז מפענחים את הבתים ביד: `response.content.decode('utf-8')`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c057",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "# חלק 2: מאלמנט אחד לכל הרשומות: find_all, מספרים וערכים חסרים\n",
    "\n",
    "דף השיעור של החלק הזה: [מאלמנט אחד לכל הרשומות: find_all, מספרים וערכים חסרים](https://dvirross.com/he/courses/intro-data-science/data-collection-find-all)\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c058",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "בשיעור הזה נלמד איך Beautiful Soup אוספת את כל האלמנטים שמתאימים, איך הבנת רשימה (list comprehension) הופכת אותם לנתונים, איך טקסט שמחזיק מספר הופך למספר, ומה עושים כשאלמנט חסר בחלק מהרשומות.\n",
    "\n",
    "## לאיזה מאמר יש הכי הרבה מחברים?\n",
    "\n",
    "מאמרים בארכיטקטורת חומרה הם לעתים קרובות עבודה של צוות: לחלקם מחבר אחד ולחלקם עשרה. ברשימה של arXiv לאוקטובר 2026 אנחנו שואלים כמה מחברים יש למאמר, לאיזה מאמר יש הכי הרבה, וכמה יש בממוצע. העותק השמור של הרשימה מחזיק את 25 הרשומות הראשונות מתוך 266, ולכן התשובות מתארות את 25 המאמרים האלה ולא את החודש כולו.\n",
    "\n",
    "התיבה הראשונה מבקשת את העותק השמור של הרשימה ובונה את ה־`soup`, כמו ב[מדף אינטרנט לנתונים: Beautiful Soup](https://dvirross.com/he/courses/intro-data-science/data-collection-beautifulsoup), כדי שהדף הזה יעמוד בפני עצמו.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c059",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.969269Z",
     "iopub.status.busy": "2026-11-22T13:17:24.969056Z",
     "iopub.status.idle": "2026-11-22T13:17:24.990193Z",
     "shell.execute_reply": "2026-11-22T13:17:24.989225Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hardware Architecture Oct 2026\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "list_url = (\n",
    "    'https://dvirross.com/materials/courses/'\n",
    "    'intro-data-science/arxiv-cs-ar-2026-10.htm'\n",
    ")\n",
    "page_text = requests.get(list_url).text\n",
    "soup = BeautifulSoup(page_text, 'html.parser')\n",
    "print(soup.title.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c060",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## `find_all`: כל האלמנטים שמתאימים\n",
    "\n",
    "כל מאמר ברשימה הוא אלמנט `dd` שמחזיק את הכותרת, את המחברים ואת הנושאים שלו. השיטה `find_all` מחזירה את כל האלמנטים שמתאימים, כרשימה, בסדר של הדף. היא מקבלת את אותם ארגומנטים כמו `find`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c061",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.991853Z",
     "iopub.status.busy": "2026-11-22T13:17:24.991678Z",
     "iopub.status.idle": "2026-11-22T13:17:24.995467Z",
     "shell.execute_reply": "2026-11-22T13:17:24.994415Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n"
     ]
    }
   ],
   "source": [
    "entries = soup.find_all('dd')\n",
    "print(len(entries))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c062",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "ברשימה `entries` יש 25 אלמנטים, אחד לכל מאמר, ולכל אחד מהם `find` משלו שמחפשת רק בתוכו. הצעדים שבהם נקרא את הכותרת של מאמר אחד ייכנסו לפונקציה, `title_of`. [הבנת רשימה](https://dvirross.com/he/courses/intro-data-science/glossary#list-comprehension) בונה רשימה מרשימה אחרת בשורה אחת: הביטוי שלפני `for` מחושב לכל אלמנט, כאן הקריאה ל־`title_of`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c063",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:24.996983Z",
     "iopub.status.busy": "2026-11-22T13:17:24.996823Z",
     "iopub.status.idle": "2026-11-22T13:17:25.001343Z",
     "shell.execute_reply": "2026-11-22T13:17:25.000265Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n",
      "SyntheticHLS: Building Diverse Synthetic High-Level Synthesis Datasets using LLMs\n",
      "ShatterQuant: Breaking Uniform Precision with Block-Wise Mixed-Precision on a Systolic Transformer Hardware Accelerator\n"
     ]
    }
   ],
   "source": [
    "def title_of(entry):\n",
    "    box = entry.find('div', class_='list-title')\n",
    "    return box.text.replace('Title:', '').strip()\n",
    "\n",
    "titles = [title_of(entry) for entry in entries]\n",
    "print(len(titles))\n",
    "print(titles[0])\n",
    "print(titles[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c064",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## סופרים את המחברים\n",
    "\n",
    "המחברים של מאמר נמצאים ב־`div` מהסוג `list-authors`, וכל מחבר הוא קישור, אלמנט `a`. לכן מספר המחברים הוא מספר הקישורים בתוך ה־`div` הזה: `find_all` מחזירה רשימה ו־`len` סופרת אותה. הריצו את התיבה והסתכלו על התוצאה למאמר השני, `entries[1]`: מספר הקישורים ב־`list-authors` שלו.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c065",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.002820Z",
     "iopub.status.busy": "2026-11-22T13:17:25.002674Z",
     "iopub.status.idle": "2026-11-22T13:17:25.006185Z",
     "shell.execute_reply": "2026-11-22T13:17:25.005224Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "authors = entries[1].find('div', class_='list-authors')\n",
    "print(len(authors.find_all('a')))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c066",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "הצעדים שבהם נקרא מאמר אחד זהים לכל המאמרים, ולכן נשים אותם בפונקציה, שמחזירה את הזוג של הכותרת ומספר המחברים, ונקרא לה על כל רשומה. זוג כזה הוא [טאפל](https://dvirross.com/he/courses/intro-data-science/glossary#tuple) (tuple).\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c067",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.007670Z",
     "iopub.status.busy": "2026-11-22T13:17:25.007525Z",
     "iopub.status.idle": "2026-11-22T13:17:25.018058Z",
     "shell.execute_reply": "2026-11-22T13:17:25.012896Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('SyntheticHLS: Building Diverse Synthetic High-Level Synthesis Datasets using LLMs', 3)\n"
     ]
    }
   ],
   "source": [
    "def read_entry(entry):\n",
    "    authors = entry.find('div', class_='list-authors')\n",
    "    return (title_of(entry), len(authors.find_all('a')))\n",
    "\n",
    "papers = [read_entry(entry) for entry in entries]\n",
    "print(papers[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c068",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **ללימוד עצמי, חשיבות 1.** קטע ללימוד עצמי: הוא אינו נלמד בהרצאה, ונקרא אותו בעצמנו. המקסימום של רשימת זוגות והממוצע של רשימת מספרים. הקטע מסייע בנושא: מקסימום וממוצע על רשימת ערכים שנאספו.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c069",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## המקסימום והממוצע\n",
    "\n",
    "`papers` היא רשימה של 25 זוגות. הפונקציה `max` מחזירה את הפריט הגדול ביותר, והארגומנט `key` שלה אומר במה להשוות: כאן בפונקציה שמקבלת זוג ומחזירה את החלק השני שלו, מספר המחברים, שנכתבת `lambda paper: paper[1]`. הממוצע הוא הסכום חלקי הכמות, ו־`round(number, 2)` משאירה שתי ספרות אחרי הנקודה העשרונית.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c070",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.019813Z",
     "iopub.status.busy": "2026-11-22T13:17:25.019652Z",
     "iopub.status.idle": "2026-11-22T13:17:25.023796Z",
     "shell.execute_reply": "2026-11-22T13:17:25.022911Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 PEEK: Heterogeneous Parallelism for Privileged Error Detection in Safety-Critical Processors\n",
      "4.2\n"
     ]
    }
   ],
   "source": [
    "top = max(papers, key=lambda paper: paper[1])\n",
    "print(top[1], top[0])\n",
    "\n",
    "counts = [paper[1] for paper in papers]\n",
    "print(round(sum(counts) / len(counts), 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c071",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "בין 25 המאמרים האלה הצוות הגדול ביותר מונה 10 מחברים, והממוצע הוא 4.2 מחברים למאמר. הממוצע גבוה ממאמר טיפוסי: למחצית מהמאמרים יש 4 מחברים או פחות, ומעט צוותים גדולים מעלים את הממוצע.\n",
    "\n",
    "## טקסט שמחזיק מספר\n",
    "\n",
    "הדף אומר כמה רשומות יש בחודש, בשורה `Total of 266 entries`, ב־`div` מהסוג `paging`. הטקסט אינו מספר, ותוכנית לא יכולה לחשב איתו. השיטה `split()` חותכת טקסט ברווחים ומחזירה רשימה של המילים שלו; `[2]`, המילה השלישית, כשסופרים מ־0, היא `'266'`, והפונקציה `int` ממירה את הטקסט של מספר שלם למספר.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c072",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.025813Z",
     "iopub.status.busy": "2026-11-22T13:17:25.025663Z",
     "iopub.status.idle": "2026-11-22T13:17:25.029534Z",
     "shell.execute_reply": "2026-11-22T13:17:25.028304Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "266\n",
      "25 of 266\n"
     ]
    }
   ],
   "source": [
    "paging = soup.find('div', class_='paging')\n",
    "total = int(paging.text.split()[2])\n",
    "print(total)\n",
    "print(len(entries), 'of', total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c073",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "בלי `int` התוכנית הייתה מחזיקה את הטקסט `'266'`, ו־`'266' + 1` היה שגיאה. מספר שנכתב עם מפריד אלפים, כמו `3,330`, צריך עוד צעד, שהוא `replace(',', '')`, כי `int` לא קוראת את הפסיק.\n",
    "\n",
    "מספר עשרוני נמיר עם `float`, ואת היחידה שכתובה אחריו נסיר קודם. הדף של מאמר כותב את הגודל של ההגשה שלו כ־`3,330 KB`, ואותם צעדים נותנים את המספר:\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c074",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.031017Z",
     "iopub.status.busy": "2026-11-22T13:17:25.030867Z",
     "iopub.status.idle": "2026-11-22T13:17:25.034547Z",
     "shell.execute_reply": "2026-11-22T13:17:25.033619Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3330.0\n"
     ]
    }
   ],
   "source": [
    "size_text = '3,330 KB'\n",
    "number = size_text.replace(' KB', '').replace(',', '')\n",
    "print(float(number))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c075",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## אלמנט שחסר\n",
    "\n",
    "לא לכל מאמר יש שורת הערות, שבה המחברים כותבים, למשל, את מספר העמודים. ברשימה, ל־13 מ־25 המאמרים אין בכלל `div` מהסוג `list-comments`. לרשומה כזו `find` מחזירה `None`, ו־`None.text` היא שגיאה. תוכנית שקוראת הרבה רשומות בודקת אם יש `None` לפני שהיא קוראת.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c076",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.036362Z",
     "iopub.status.busy": "2026-11-22T13:17:25.036115Z",
     "iopub.status.idle": "2026-11-22T13:17:25.040440Z",
     "shell.execute_reply": "2026-11-22T13:17:25.039337Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n"
     ]
    }
   ],
   "source": [
    "without_comments = [\n",
    "    entry for entry in entries\n",
    "    if entry.find('div', class_='list-comments') is None\n",
    "]\n",
    "print(len(without_comments))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c077",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "כשההערות קיימות, הן טקסט חופשי שהמחברים כתבו, כמו `7 pages, 5 figures. Accepted to ASP-DAC 2027`. תוכנית קוראת מספר עמודים רק במקום שהטקסט אומר אותו, בצורה שמספר בא קודם והמילה `pages` אחריו. השיטה `isdigit()` אומרת אם טקסט מורכב מספרות בלבד, ו־`startswith('pages')` אם טקסט מתחיל במילה. הפונקציה מחזירה `None` לכל רשומה שאין בה מספר עמודים.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c078",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.042517Z",
     "iopub.status.busy": "2026-11-22T13:17:25.042259Z",
     "iopub.status.idle": "2026-11-22T13:17:25.046578Z",
     "shell.execute_reply": "2026-11-22T13:17:25.045426Z"
    }
   },
   "outputs": [],
   "source": [
    "def pages_of(entry):\n",
    "    box = entry.find('div', class_='list-comments')\n",
    "    if box is None:\n",
    "        return None\n",
    "    words = box.text.replace('Comments:', '').split()\n",
    "    if len(words) < 2:\n",
    "        return None\n",
    "    if words[0].isdigit() and words[1].startswith('pages'):\n",
    "        return int(words[0])\n",
    "    return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c079",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.048521Z",
     "iopub.status.busy": "2026-11-22T13:17:25.048359Z",
     "iopub.status.idle": "2026-11-22T13:17:25.052711Z",
     "shell.execute_reply": "2026-11-22T13:17:25.051604Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[None, None, 7, None, 4, 4, None, None, None, None, 8, None, None, None, 10, 15, None, None, None, None, None, None, None, None, None]\n"
     ]
    }
   ],
   "source": [
    "all_pages = [pages_of(entry) for entry in entries]\n",
    "print(all_pages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c080",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "מספר חסר אינו אפס. כדי לחשב עם המספרים שקיימים, נשמור רק את הפריטים שאינם `None`, ונחשב עליהם את הממוצע:\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c081",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.054610Z",
     "iopub.status.busy": "2026-11-22T13:17:25.054425Z",
     "iopub.status.idle": "2026-11-22T13:17:25.058376Z",
     "shell.execute_reply": "2026-11-22T13:17:25.057241Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 15 8.0\n"
     ]
    }
   ],
   "source": [
    "known = [pages for pages in all_pages if pages is not None]\n",
    "print(len(known), max(known), sum(known) / len(known))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c082",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "רק 6 מ־25 המאמרים מציינים כמה עמודים יש להם, ולאלה 8 עמודים בממוצע ולכל היותר 15. המספר מתאר את המאמרים שמציינים, והם עשויים להיות שונים מהמאמרים שלא.\n",
    "\n",
    "### תרגול\n",
    "\n",
    "**נסו בעצמכם.** הרשימה `papers` מחזיקה זוג של כותרת ומספר מחברים לכל מאמר. הדפיסו את הכותרת של המאמר שיש לו הכי מעט מחברים; כשלכמה מאמרים יש הכי מעט, הדפיסו את הראשון שבהם ברשימה. הפונקציה `min` מקבלת את אותו ארגומנט `key` כמו `max`; שתיהן הופיעו ב[Python למסדי נתונים 2: לולאות ופונקציות](https://dvirross.com/he/courses/intro-data-science/python-loops-and-functions).\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c083",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.059854Z",
     "iopub.status.busy": "2026-11-22T13:17:25.059696Z",
     "iopub.status.idle": "2026-11-22T13:17:25.062650Z",
     "shell.execute_reply": "2026-11-22T13:17:25.061522Z"
    }
   },
   "outputs": [],
   "source": [
    "# Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c084",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**פלט צפוי (המספרים מוסתרים)**\n",
    "\n",
    "```text\n",
    "A Compression Tree Generation Method for High Performance VLSI Datapaths\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c085",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.064377Z",
     "iopub.status.busy": "2026-11-22T13:17:25.064184Z",
     "iopub.status.idle": "2026-11-22T13:17:25.067898Z",
     "shell.execute_reply": "2026-11-22T13:17:25.066662Z"
    }
   },
   "outputs": [],
   "source": [
    "check(4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c086",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **רמז**\n",
    ">\n",
    "> הפונקציה `min` פועלת כמו `max`, עם אותו ארגומנט `key`. היא מחזירה את הראשון מבין הפריטים שיש להם את הערך הקטן ביותר.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c087",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **ללימוד עצמי, חשיבות 2.** קטע ללימוד עצמי: הוא אינו נלמד בהרצאה, ונקרא אותו בעצמנו. עוד שתי משימות תרגול: ספירה וממוצע. הקטע מסייע בנושא: ספירת פריטים לפי תנאי, וממוצע שמדלג על ערך חסר.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c088",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**נסו בעצמכם.** הדפיסו בכמה מ־25 המאמרים יש לפחות 5 מחברים, כמספר אחד.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c089",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.069565Z",
     "iopub.status.busy": "2026-11-22T13:17:25.069367Z",
     "iopub.status.idle": "2026-11-22T13:17:25.071867Z",
     "shell.execute_reply": "2026-11-22T13:17:25.071327Z"
    }
   },
   "outputs": [],
   "source": [
    "# Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c090",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**פלט צפוי (המספרים מוסתרים)**\n",
    "\n",
    "```text\n",
    "<number>\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c091",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.074554Z",
     "iopub.status.busy": "2026-11-22T13:17:25.074232Z",
     "iopub.status.idle": "2026-11-22T13:17:25.077588Z",
     "shell.execute_reply": "2026-11-22T13:17:25.076641Z"
    }
   },
   "outputs": [],
   "source": [
    "check(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c092",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **רמז**\n",
    ">\n",
    "> בנו רשימה של הזוגות שהחלק השני שלהם הוא לפחות 5, עם תנאי בסוף של הבנת רשימה, והדפיסו את האורך שלה.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c093",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**נסו בעצמכם.** הפונקציה `pages_of` שלמעלה מחזירה את מספר העמודים של רשומה, או `None`. הדפיסו את הממוצע של מספר העמודים, מעוגל לספרה אחת אחרי הנקודה העשרונית, על המאמרים שמציינים כמה עמודים יש להם.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c094",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.080593Z",
     "iopub.status.busy": "2026-11-22T13:17:25.080428Z",
     "iopub.status.idle": "2026-11-22T13:17:25.083854Z",
     "shell.execute_reply": "2026-11-22T13:17:25.082393Z"
    }
   },
   "outputs": [],
   "source": [
    "# Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c095",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**פלט צפוי (המספרים מוסתרים)**\n",
    "\n",
    "```text\n",
    "<number>.<number>\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c096",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.085678Z",
     "iopub.status.busy": "2026-11-22T13:17:25.085508Z",
     "iopub.status.idle": "2026-11-22T13:17:25.089224Z",
     "shell.execute_reply": "2026-11-22T13:17:25.087928Z"
    }
   },
   "outputs": [],
   "source": [
    "check(6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c097",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **רמז**\n",
    ">\n",
    "> שמרו רק את הערכים שאינם `None`, כמו ברשימה `known`, וחלקו את הסכום שלהם בכמותם, לא במספר הרשומות.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c098",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## נקודות שכדאי לזכור\n",
    "\n",
    "- `find_all` מחזירה רשימה של כל האלמנטים שמתאימים; לאלמנט ברשימה יש `find` ו־`find_all` משלו.\n",
    "- הבנת רשימה הופכת את רשימת האלמנטים לרשימה של נתונים; פונקציה שקוראת רשומה אחת שומרת את הקוד במקום אחד.\n",
    "- הטקסט של אלמנט הוא טקסט: `int(...)` ממירה טקסט של מספר שלם, `float(...)` מספר עם נקודה עשרונית, `split()` חותכת טקסט למילים, ויחידה או מפריד אלפים נסיר קודם עם `replace`.\n",
    "- אלמנט שחסר נותן `None` מ־`find`; נבדוק `is None` לפני קריאת `text`, ונשאיר ערך חסר מחוץ לממוצע, כי הוא אינו אפס.\n",
    "- `max` ו־`min` עם `key` מחזירות את הפריט שיש לו הערך הגדול או הקטן ביותר של חלק ממנו.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c099",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "# חלק 3: מרשימה לדפים רבים: עוקבים אחרי קישורים\n",
    "\n",
    "דף השיעור של החלק הזה: [מרשימה לדפים רבים: עוקבים אחרי קישורים](https://dvirross.com/he/courses/intro-data-science/data-collection-linked-pages)\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c100",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **ללימוד עצמי.** דף ללימוד עצמי: הוא אינו נלמד בהרצאה, ונקרא אותו בעצמנו.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c101",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "בשיעור הזה נלמד איך תוכנית עוקבת אחרי הקישורים של רשימה אל הדף של כל פריט, קוראת נתונים מהתגיות שמתארות דף, הופכת טקסט כמו `3,330 KB` למספר, ואוספת את התוצאות של דפים רבים ברשימה אחת.\n",
    "\n",
    "## מה גודל ההגשה של כל מאמר?\n",
    "\n",
    "הרשימה של המאמרים מראה את הכותרת, המחברים והנושאים של כל מאמר, אבל לא את הגודל של הקבצים שהמחברים הגישו. המספר הזה נמצא בדף של כל מאמר, שהרשימה מקשרת אליו. השאלה היא: בין שמונת המאמרים הראשונים ברשימה, לאיזה יש ההגשה הגדולה ביותר, ומה הגודל החציוני? כדי לענות, תוכנית צריכה לקרוא את הרשימה, לעקוב אחרי שמונה קישורים ולקרוא מספר אחד בכל דף.\n",
    "\n",
    "התיבה הראשונה מגדירה את `soup_of`, פונקציה שמבקשת כתובת ומחזירה את ה־`soup` של הדף שלה, כמו ב[מדף אינטרנט לנתונים: Beautiful Soup](https://dvirross.com/he/courses/intro-data-science/data-collection-beautifulsoup), כדי שהדף הזה יעמוד בפני עצמו. היא בונה את ה־`soup` של הרשימה ואוספת את הקישורים למאמרים: כל קישור שהתכונה `title` שלו היא `Abstract`, כמו ב[מדף אינטרנט לנתונים: Beautiful Soup](https://dvirross.com/he/courses/intro-data-science/data-collection-beautifulsoup). חיתוך, `[:8]`, משאיר את שמונת הראשונים.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "c102",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.091040Z",
     "iopub.status.busy": "2026-11-22T13:17:25.090753Z",
     "iopub.status.idle": "2026-11-22T13:17:25.112867Z",
     "shell.execute_reply": "2026-11-22T13:17:25.111912Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/abs/2610.00106\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "copies = (\n",
    "    'https://dvirross.com/materials/courses/'\n",
    "    'intro-data-science/'\n",
    ")\n",
    "\n",
    "def soup_of(address):\n",
    "    text = requests.get(address).text\n",
    "    return BeautifulSoup(text, 'html.parser')\n",
    "\n",
    "soup = soup_of(copies + 'arxiv-cs-ar-2026-10.htm')\n",
    "anchors = soup.find_all('a', title='Abstract')\n",
    "links = [a.get('href') for a in anchors][:8]\n",
    "print(links[0])\n",
    "print(len(links))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c103",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## מהקישור אל הכתובת של דף\n",
    "\n",
    "ה־`href` של מאמר הוא `/abs/2610.00106`: המזהה של המאמר הוא החלק האחרון שלו, והכתובת יחסית לאתר של הרשימה, `https://arxiv.org`. באתר החי הכתובת המלאה היא שם האתר ואחריו ה־`href`. העותקים השמורים של המאמרים נשמרים בשמות מהצורה `arxiv-abs-2610.00106.htm`, ולכן פונקציה הופכת `href` לכתובת של העותק שלו.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c104",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.114725Z",
     "iopub.status.busy": "2026-11-22T13:17:25.114556Z",
     "iopub.status.idle": "2026-11-22T13:17:25.131877Z",
     "shell.execute_reply": "2026-11-22T13:17:25.130816Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title:SyntheticHLS: Building Diverse Synthetic High-Level Synthesis Datasets using LLMs\n"
     ]
    }
   ],
   "source": [
    "def copy_address(href):\n",
    "    paper_id = href.replace('/abs/', '')\n",
    "    return copies + 'arxiv-abs-' + paper_id + '.htm'\n",
    "\n",
    "page = soup_of(copy_address(links[0]))\n",
    "print(page.find('h1', class_='title').text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c105",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## נתונים בתגיות שמתארות את הדף\n",
    "\n",
    "בראש כל דף יש אלמנטים `meta`: תגיות שלא מצוירות, שמתארות את הדף למנועי חיפוש ולתוכנות. לכל אחד יש תכונה `name` שאומרת מה הוא מחזיק, ותכונה `content` שמחזיקה את הערך, למשל התאריך שבו המאמר הוגש. הפרמטר הראשון של `find` נקרא גם הוא `name`, ולכן הכתיבה `name='citation_date'` זורקת `TypeError`; התכונה נכתבת במילון `attrs`, עם השם שאנחנו רוצים; `get('content')` קוראת את הערך שלו.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "c106",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.133521Z",
     "iopub.status.busy": "2026-11-22T13:17:25.133342Z",
     "iopub.status.idle": "2026-11-22T13:17:25.137526Z",
     "shell.execute_reply": "2026-11-22T13:17:25.136244Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2026/09/09\n"
     ]
    }
   ],
   "source": [
    "date = page.find('meta', attrs={'name': 'citation_date'})\n",
    "print(date.get('content'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c107",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "בדף יש אלמנט `meta` אחד לכל מחבר, עם השם `citation_author`, ולכן `find_all` עם אותו `attrs` סופרת את המחברים:\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c108",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.139672Z",
     "iopub.status.busy": "2026-11-22T13:17:25.139492Z",
     "iopub.status.idle": "2026-11-22T13:17:25.143440Z",
     "shell.execute_reply": "2026-11-22T13:17:25.142513Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "meta_name = {'name': 'citation_author'}\n",
    "print(len(page.find_all('meta', attrs=meta_name)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c109",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## מספר בתוך טקסט\n",
    "\n",
    "הגודל של ההגשה כתוב בחלק של הדף שנקרא היסטוריית ההגשות, ב־`div` מהסוג `submission-history`, כחלק משורת טקסט: `[v1] Wed, 9 Sep 2026 02:40:29 UTC (3,330 KB)`. המספר נמצא בין סוגר לבין האותיות `KB`, ויש בו פסיק. נוציא אותו בשלושה צעדים. `split('(')` חותכת את הטקסט בכל סוגר פותח ומחזירה רשימה של החלקים, ו־`[-1]`, החלק האחרון, הוא `'3,330 KB)'` עם מעבר שורה. לכל המאמרים האלה גרסה אחת; עם כמה גרסאות החלק האחרון הוא הגודל של האחרונה. `replace` מסירה את האותיות ואת הפסיק, `strip` מסירה את מעבר השורה, ו־`int` ממירה את הטקסט למספר. הפונקציה `repr` מראה טקסט כפי שהוא כתוב, עם התווים הנסתרים שבו, כמו מעבר השורה `\\n`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "c110",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.145912Z",
     "iopub.status.busy": "2026-11-22T13:17:25.145746Z",
     "iopub.status.idle": "2026-11-22T13:17:25.150583Z",
     "shell.execute_reply": "2026-11-22T13:17:25.148706Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'3,330 KB)\\n'\n",
      "3330\n"
     ]
    }
   ],
   "source": [
    "box = page.find('div', class_='submission-history')\n",
    "history = box.text\n",
    "piece = history.split('(')[-1]\n",
    "print(repr(piece))\n",
    "number = piece.replace('KB)', '').replace(',', '')\n",
    "kb = int(number.strip())\n",
    "print(kb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c111",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "בלי הסרת הפסיק, `int('3,330')` מעלה שגיאה.\n",
    "\n",
    "## אלמנט חסר עוצר את התוכנית\n",
    "\n",
    "בחלק מהמאמרים יש בטבלה של הדף שורת הערות, אלמנט `td` מהסוג `comments`, ובחלקם אין. ההערה של המאמר הראשון קיימת; בדף של המאמר השני אין. לבקש את הטקסט של אלמנט שאינו שם זו שגיאה:\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c112",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "בדף השיעור התא שלמעלה נעצר בשגיאה בכוונה. במחברת התא שלמטה מדפיס את האלמנט ש־`find` החזירה, כדי שהמחברת תרוץ עד הסוף: הוא `None`, והשגיאה באה מכך שביקשנו מ־`None` את `.text`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "c113",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.152146Z",
     "iopub.status.busy": "2026-11-22T13:17:25.151934Z",
     "iopub.status.idle": "2026-11-22T13:17:25.157258Z",
     "shell.execute_reply": "2026-11-22T13:17:25.155359Z"
    }
   },
   "outputs": [],
   "source": [
    "second = soup_of(copy_address(links[1]))\n",
    "print(second.find('td', class_='comments'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c114",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "`find` החזירה `None`, ול־`None` אין תכונה `text`. פונקציה שקוראת דף בודקת את האלמנט לפני שהיא קוראת ממנו, ומחזירה `None` בשביל הערך שלא קיים. הפונקציה שלמטה קוראת מאמר אחד ומחזירה את הנתונים שלו כמילון: המזהה, הכותרת מתגית ה־`meta`, הגודל ב־KB, וההערה או `None`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c115",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.162280Z",
     "iopub.status.busy": "2026-11-22T13:17:25.162055Z",
     "iopub.status.idle": "2026-11-22T13:17:25.188021Z",
     "shell.execute_reply": "2026-11-22T13:17:25.186991Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19279 None\n"
     ]
    }
   ],
   "source": [
    "def read_paper(href):\n",
    "    page = soup_of(copy_address(href))\n",
    "    meta = {'name': 'citation_title'}\n",
    "    title = page.find('meta', attrs=meta).get('content')\n",
    "    box = page.find('div', class_='submission-history')\n",
    "    piece = box.text.split('(')[-1]\n",
    "    number = piece.replace('KB)', '').replace(',', '')\n",
    "    kb = int(number.strip())\n",
    "    comment = page.find('td', class_='comments')\n",
    "    if comment is not None:\n",
    "        comment = comment.text.strip()\n",
    "    return {\n",
    "        'id': href.replace('/abs/', ''),\n",
    "        'title': title, 'kb': kb, 'comment': comment,\n",
    "    }\n",
    "\n",
    "second_paper = read_paper(links[1])\n",
    "print(second_paper['kb'], second_paper['comment'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c116",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## כל שמונת הדפים\n",
    "\n",
    "לולאת `for`, או הבנת רשימה, קוראת לפונקציה על כל קישור. תוכנית ששולחת הרבה בקשות לאתר אחד ממתינה ביניהן, עם `time.sleep(seconds)`, כפי ש[לקרוא דפים באחריות](https://dvirross.com/he/courses/intro-data-science/data-collection-beautifulsoup#לקרוא-דפים-באחריות) מסביר; העותקים באתר הזה אינם arXiv, ולכן התיבה לא צריכה הפסקה.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "c117",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.190117Z",
     "iopub.status.busy": "2026-11-22T13:17:25.189929Z",
     "iopub.status.idle": "2026-11-22T13:17:25.309713Z",
     "shell.execute_reply": "2026-11-22T13:17:25.308134Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2610.00106 3330\n",
      "2610.00207 19279\n",
      "2610.00311 856\n",
      "2610.01186 866\n",
      "2610.01603 492\n",
      "2610.01623 234\n",
      "2610.01867 747\n",
      "2610.01918 159\n"
     ]
    }
   ],
   "source": [
    "papers = [read_paper(href) for href in links]\n",
    "for paper in papers:\n",
    "    print(paper['id'], paper['kb'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c118",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "השאלה של השיעור נענית במקסימום, בממוצע ובחציון של הגדלים. החציון הוא האמצע של הגדלים לפי הסדר: `sorted` מסדרת אותם, ובשמונה ערכים הוא הממוצע של שני האמצעיים, במקומות 3 ו־4 (כשסופרים מ־0).\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "c119",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.312022Z",
     "iopub.status.busy": "2026-11-22T13:17:25.311803Z",
     "iopub.status.idle": "2026-11-22T13:17:25.316313Z",
     "shell.execute_reply": "2026-11-22T13:17:25.315366Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19279 ShatterQuant: Breaking Uniform Precision\n",
      "3245\n",
      "801.5\n"
     ]
    }
   ],
   "source": [
    "sizes = [paper['kb'] for paper in papers]\n",
    "largest = max(papers, key=lambda paper: paper['kb'])\n",
    "print(largest['kb'], largest['title'][:40])\n",
    "print(round(sum(sizes) / len(sizes)))\n",
    "ordered = sorted(sizes)\n",
    "print((ordered[3] + ordered[4]) / 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c120",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "ההגשה הגדולה ביותר היא 19,279 KB, הממוצע הוא 3,245 KB והחציון הוא 801.5 KB. מאמר גדול אחד מושך את הממוצע למעלה (לשבעת המאמרים האחרים ממוצע של 955 KB) אבל לא את החציון, ולכן החציון מתאר מאמר טיפוסי.\n",
    "\n",
    "### תרגול\n",
    "\n",
    "**נסו בעצמכם.** הפונקציה `read_paper` והרשימה `papers` נמצאות למעלה. הדפיסו בכמה משמונת המאמרים אין הערה בדף.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "c121",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.318809Z",
     "iopub.status.busy": "2026-11-22T13:17:25.318636Z",
     "iopub.status.idle": "2026-11-22T13:17:25.321976Z",
     "shell.execute_reply": "2026-11-22T13:17:25.320736Z"
    }
   },
   "outputs": [],
   "source": [
    "# Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c122",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**פלט צפוי (המספרים מוסתרים)**\n",
    "\n",
    "```text\n",
    "3\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c123",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.326134Z",
     "iopub.status.busy": "2026-11-22T13:17:25.325937Z",
     "iopub.status.idle": "2026-11-22T13:17:25.330279Z",
     "shell.execute_reply": "2026-11-22T13:17:25.328972Z"
    }
   },
   "outputs": [],
   "source": [
    "check(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c124",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **רמז**\n",
    ">\n",
    "> ב־`papers`, למאמר בלי הערה יש `None` במפתח `'comment'`. ספרו עם הבנת רשימה ותנאי `is None`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c125",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**נסו בעצמכם.** העותק השמור של הדף של המאמר `2610.00311` נמצא ב־`copies + 'arxiv-abs-2610.00311.htm'`. הדפיסו את התאריך של ההגשה שלו, מתגית ה־`meta` מהסוג `citation_date`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "c126",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.332106Z",
     "iopub.status.busy": "2026-11-22T13:17:25.331942Z",
     "iopub.status.idle": "2026-11-22T13:17:25.335014Z",
     "shell.execute_reply": "2026-11-22T13:17:25.333858Z"
    }
   },
   "outputs": [],
   "source": [
    "# Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c127",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**פלט צפוי (המספרים מוסתרים)**\n",
    "\n",
    "```text\n",
    "<number>/<number>/<number>\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "c128",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.337294Z",
     "iopub.status.busy": "2026-11-22T13:17:25.337055Z",
     "iopub.status.idle": "2026-11-22T13:17:25.340913Z",
     "shell.execute_reply": "2026-11-22T13:17:25.339849Z"
    }
   },
   "outputs": [],
   "source": [
    "check(8)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c129",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **רמז**\n",
    ">\n",
    "> בנו את ה־`soup` של הדף, מצאו את אלמנט ה־`meta` עם `attrs={'name': 'citation_date'}` (ולא `name='citation_date'`, שזורקת `TypeError`) וקראו את התכונה `content` שלו עם `get`.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c130",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**נסו בעצמכם.** הדפיסו את הגודל הממוצע ב־KB של שבעת המאמרים מלבד הגדול ביותר, מעוגל למספר שלם.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c131",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.342818Z",
     "iopub.status.busy": "2026-11-22T13:17:25.342667Z",
     "iopub.status.idle": "2026-11-22T13:17:25.346194Z",
     "shell.execute_reply": "2026-11-22T13:17:25.344820Z"
    }
   },
   "outputs": [],
   "source": [
    "# Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c132",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**פלט צפוי (המספרים מוסתרים)**\n",
    "\n",
    "```text\n",
    "<number>\n",
    "```\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "c133",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.348726Z",
     "iopub.status.busy": "2026-11-22T13:17:25.348555Z",
     "iopub.status.idle": "2026-11-22T13:17:25.352758Z",
     "shell.execute_reply": "2026-11-22T13:17:25.351513Z"
    }
   },
   "outputs": [],
   "source": [
    "check(9)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c134",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "> **רמז**\n",
    ">\n",
    "> קחו את הגדלים ב־`sizes`, הסירו את הגדול ביותר, `max(sizes)`, בתנאי בהבנת רשימה, וחלקו את הסכום של האחרים בכמותם.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c135",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "**האתר החי.** התא שלמטה יכול לקרוא מ־arXiv עצמו את הדפים של שלושת המאמרים הראשונים ברשימה, עם סוכן משתמש שאומר איך אפשר ליצור איתנו קשר וההפסקה ש־`robots.txt` מבקש, 15 שניות בין בקשות; זה לוקח בערך דקה. הוא כבוי (`RUN_LIVE = False`): כיתה שלמה שמריצה אותו יחד שולחת הרבה בקשות מאותה כתובת. אדם אחד, פעם אחת, יכול לשנות ל־`RUN_LIVE = True`. אם האתר עונה 403 או 429, התא עוצר ואומר זאת, והוא לא מנסה שוב. שאר המחברת עובדת על העותקים השמורים.\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "c136",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-22T13:17:25.355005Z",
     "iopub.status.busy": "2026-11-22T13:17:25.354844Z",
     "iopub.status.idle": "2026-11-22T13:17:25.358412Z",
     "shell.execute_reply": "2026-11-22T13:17:25.357094Z"
    }
   },
   "outputs": [],
   "source": [
    "import time\n",
    "RUN_LIVE = False  # switch to True to ask arXiv itself (four requests, 15 seconds apart)\n",
    "if RUN_LIVE:\n",
    "    live_headers = {'User-Agent': 'IntroDataScienceCourse/1.0 (teaching example; contact via dvirross.com)'}\n",
    "    live_list = requests.get('https://arxiv.org/list/cs.AR/2026-10?skip=0&show=25', headers=live_headers, timeout=30)\n",
    "    refused = live_list.status_code in (403, 429)\n",
    "    if not refused:\n",
    "        live_soup = BeautifulSoup(live_list.text, 'html.parser')\n",
    "        for link in live_soup.find_all('a', title='Abstract')[:3]:\n",
    "            if not refused:\n",
    "                time.sleep(15)\n",
    "                live_page = requests.get('https://arxiv.org' + link.get('href'), headers=live_headers, timeout=30)\n",
    "                print(live_page.status_code, link.get('href'))\n",
    "                refused = live_page.status_code in (403, 429)\n",
    "    if refused:\n",
    "        print('The site refused a request or asked to slow down (status 403 or 429). Do not try again now; wait and use the saved copies.')\n",
    "else:\n",
    "    print('The live cell is off. Set RUN_LIVE = True to run it.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c137",
   "metadata": {},
   "source": [
    "<div dir=\"rtl\">\n",
    "\n",
    "## נקודות שכדאי לזכור\n",
    "\n",
    "- דף רשימה מקשר אל הדף של כל פריט; ה־`href` של קישור הוא הדרך, ופונקציה שהופכת `href` לכתובת שומרת את הקוד במקום אחד.\n",
    "- `find('meta', attrs={'name': ...})` ו־`get('content')` קוראות את הנתונים שדף שומר בתגיות ה־`meta` שלו.\n",
    "- מספר בתוך טקסט נחתך עם `split`, מנוקה עם `replace` ו־`strip`, ומומר עם `int`.\n",
    "- אלמנט שחסר הוא `None`; לבקש ממנו `text` מעלה שגיאה, ולכן פונקציה בודקת `None` ומחזירה `None`.\n",
    "- הממוצע של כמה ערכים נמשך על ידי ערך גדול מאוד; מסתכלים על המקסימום ועל החציון לצד הממוצע.\n",
    "- תוכנית שקוראת אתר שומרת על ה־`robots.txt` שלו, ממתינה בין בקשות, אומרת מי היא ולוקחת רק את הנתונים שהיא צריכה.\n",
    "\n",
    "</div>"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "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.13.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
