HTML, BeautifulSoup, robots.txt, תנאי שימוש וקצב בקשות.
HTML, BeautifulSoup, robots.txt, terms of use and request rate.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
כותרות מדףTitles from a page
חלצו מה-HTML הנתון את כל הכותרות (בתגית b) לרשימה והדפיסו אותה.
Extract all titles (in the b tag) from the given HTML into a list and print it.
איך הפלט אמור להיראות?What should the output look like?
['Dune', 'Emma', 'Matilda']
main.py
רמזHint 1:soup.find_all("b")soup.find_all("b")
רמזHint 2:[b.get_text() for b in ...][b.get_text() for b in ...]
מדף, צעד 26: איסוף רשימה מאתר תרגולShelf, step 26: collecting a list from a practice site
פרויקט מלווהCompanion project
כתבו scrape_page(html) שמחזירה (books, next_href): books היא רשימת מילונים {title, author, price, stars} (price כ-float בלי ₪, stars כמספר 1–5 מהמילה באנגלית ב-class), ו-next_href הוא ה-href של a.next או None. בדיקה על דף התרגול המוקלט.
Write scrape_page(html) returning (books, next_href): books is a list of dicts {title, author, price, stars} (price as a float without ₪, stars as 1–5 from the English word in the class), and next_href is the a.next href or None. Tested on the recorded practice page.
איך הפלט אמור להיראות?What should the output look like?
כתבו crawl(start, fetch, can_fetch, sleep, max_pages=10): מתחילה מ-start, מביאה כל עמוד עם fetch(url) (מחזירה HTML), אוספת כותרות (article.book h3 a → title), עוקבת אחרי a.next עם urljoin, לא מבקרת פעמיים באותה כתובת, מדלגת על כתובת ש-can_fetch(url) אוסר, וקוראת ל-sleep(1) בין עמודים (לא לפני הראשון). מחזירה את רשימת הכותרות.
Write crawl(start, fetch, can_fetch, sleep, max_pages=10): starts at start, fetches each page with fetch(url) (returns HTML), collects titles (article.book h3 a → title), follows a.next with urljoin, never visits an address twice, skips an address can_fetch(url) forbids, and calls sleep(1) between pages (not before the first). Returns the list of titles.
איך הפלט אמור להיראות?What should the output look like?
sleep 1
['Dune', 'Emma']
main.py
רמזHint 1:קבוצה seen וספירת עמודים.A seen set and a page counter.
רמזHint 2:שבירת הלולאה: אין next, ביקרנו, אסור, או הגענו ל-max_pages.Stop when: no next, already visited, forbidden, or max_pages reached.
שני דפים של חנויות שונות (HTML נתון) עם אותם ספרים במבנה שונה: בחנות A div.item עם span.name ו-span.cost (59.90 ILS), בחנות B li.p עם מאפיינים data-title ו-data-price. כתבו cheapest(a_html, b_html) שמחזירה מילון כותרת → (store, price) של המחיר הזול יותר לכל ספר שמופיע בשתיהן.
Two shops' pages (given HTML) list the same books in different structures: shop A has div.item with span.name and span.cost (59.90 ILS), shop B has li.p with data-title and data-price attributes. Write cheapest(a_html, b_html) returning a dict title → (store, price) with the lower price for every book in both.
איך הפלט אמור להיראות?What should the output look like?
{'Dune': ('B', 54.5), 'Emma': ('A', 35.0)}
main.py
רמזHint 1:פונקציה לכל חנות שמחזירה מילון כותרת → מחיר.A function per shop returning title → price.