מפגש 26: HTML, BeautifulSoup, robots.txt, תנאי שימוש וקצב בקשות.Session 26: HTML, BeautifulSoup, robots.txt, terms of use and request rate.
HTML, BeautifulSoup, robots.txt, תנאי שימוש וקצב בקשות. בסוף המפגש מוסיפים צעד לפרויקט המלווה.HTML, BeautifulSoup, robots.txt, terms of use and request rate. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
HTMLHTML
soupSoup
בורריםSelectors
הבאהFetching
עמודיםPages
אחריותResponsibility
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
HTMLHTML
איך נראה דף מבפניםWhat a page looks like inside
תגיות פותחות וסוגרות: <p>...</p>Opening and closing tags: <p>...</p>
בדפדפן: קליק ימני → InspectIn the browser: right-click → Inspect
כשאין API, אפשר לקרוא את הדף עצמו: זה גירוד רשת. כל דף בנוי מHTML: תגיות שעוטפות תוכן ויוצרות עץ. לכל תגית יכולים להיות מאפיינים כמו class. כדי לגלות את המבנה, בדפדפן לוחצים קליק ימני על פריט ובוחרים Inspect. כך נראה דף התרגול שלנו (חנות מומצאת, shelf-practice.example):When there's no API, you can read the page itself: that's web scraping. Every page is built from HTML: tags that wrap content and form a tree. Tags can have attributes like class. To discover the structure, right-click an item in the browser and choose Inspect. Here's our practice page (an invented shop, shelf-practice.example):
אנלוגיהAnalogy
HTML הוא כמו בובות בבושקה: כל תגית יכולה להכיל תגיות אחרות, ובתוכן עוד. מגרדים זה לפתוח בדיוק את הבובות שצריך.HTML is like Russian nesting dolls: every tag can hold others, and those hold more. Scraping means opening exactly the dolls you need.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
find = הראשון, find_all = כולםfind = the first, find_all = all
.get_text(strip=True) ו-tag["attr"].get_text(strip=True) and tag["attr"]
BeautifulSoup הופך מחרוזת HTML לעץ שאפשר לחפש בו. find מחזיר את האלמנט הראשון שמתאים, find_all את כולם. מתוך אלמנט, get_text(strip=True) נותן את הטקסט הנקי, ו-tag["data-pages"] את ערך המאפיין.BeautifulSoup turns an HTML string into a tree you can search. find returns the first matching element, find_all returns all of them. From an element, get_text(strip=True) gives the clean text, and tag["data-pages"] the attribute's value.
אנלוגיהAnalogy
BeautifulSoup הוא כמו תוכן עניינים לדף: במקום לקרוא הכול מההתחלה, קופצים ישר לפרק שמחפשים.BeautifulSoup is like a table of contents for a page: instead of reading from the start, jump straight to the chapter you want.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
.class, #id, רווח = בתוך.class, #id, a space = inside
select_one = הראשוןselect_one = the first
בוררי CSS הם אותה שפה שבה מעצבים דפים, והם הדרך הקצרה לתאר מה רוצים: .price לפי class, #shelf לפי id, ורווח אומר "בתוך". select מחזיר רשימה ו-select_one את הראשון. ב-Inspect בדפדפן יש גם Copy → Copy selector.CSS selectors are the same language used to style pages, and the short way to describe what you want: .price by class, #shelf by id, and a space means "inside". select returns a list and select_one the first. In the browser's Inspect there's also Copy → Copy selector.
אנלוגיהAnalogy
בורר CSS הוא כמו כתובת למשלוח: עיר, רחוב, בית, דירה. ככל שמפרטים יותר, מגיעים בדיוק לדלת הנכונה.A CSS selector is like a delivery address: city, street, building, flat. The more specific, the more exactly you reach the right door.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
הבאהFetching
להביא דף אמיתיFetching a real page
requests.get(url, headers=...) ואז souprequests.get(url, headers=...) then soup
User-Agent שמזהה אתכםA User-Agent that identifies you
פונקציה אחת לדף אחדOne function per page
בפועל מביאים את הדף עם requests (מפגש 22) ומעבירים את r.text ל-BeautifulSoup. מנומס להציג את עצמכם בכותרת User-Agent. כמו במפגש 22: כאן התשובה הוקלטה מראש, ולתרגול אמיתי יש אתרים שנבנו בדיוק בשביל זה, כמו books.toscrape.com.In practice you fetch the page with requests (session 22) and pass r.text to BeautifulSoup. It's polite to introduce yourself in a User-Agent header. As in session 22: the reply here was recorded in advance, and for real practice there are sites built exactly for this, like books.toscrape.com.
אנלוגיהAnalogy
להציג User-Agent זה כמו להזדהות בכניסה לבניין: השומר יודע מי נכנס ולמה, וקל לו יותר לאשר.Sending a User-Agent is like signing in at a building's front desk: the guard knows who's coming and why, and is happier to let you in.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
fetch.py
print(float("₪59.90"))
מה Python עונהWhat Python says
ValueError: could not convert string to float: '₪59.90'
ValueError: could not convert string to float: '₪59.90': מנקים קודם: .lstrip("₪") (או replace).ValueError: could not convert string to float: '₪59.90': clean it first: .lstrip("₪") (or replace).
ממירים מחיר עם סימן מטבע.Converting a price that still has its currency sign.
נושאTopic 5
עמודיםPages
כמה עמודים ושמירהSeveral pages and saving
עוקבים אחרי קישור "next" עד שנגמרFollow the "next" link until it runs out
urljoin לכתובות יחסיותurljoin for relative addresses
שומרים ל-CSV (מפגש 21)Save to CSV (session 21)
אתרים מחלקים רשימות לעמודים. לולאת while מביאה עמוד, אוספת, ומחפשת קישור next. הקישור הוא לרוב יחסי (page-2.html), ו-urljoin הופך אותו לכתובת מלאה. בסוף שומרים הכול ל-CSV, כמו במפגש 21.Sites split lists into pages. A while loop fetches a page, collects, and looks for a next link. The link is usually relative (page-2.html), and urljoin turns it into a full address. Finally save everything to CSV, as in session 21.
אנלוגיהAnalogy
לעבור עמודים זה כמו לדפדף בקטלוג: קוראים עמוד, מחפשים "המשך בעמוד הבא", ועוצרים כשאין יותר.Paging is like leafing through a catalogue: read a page, look for "continued on the next page", and stop when there's none.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
אחריותResponsibility
לגרד באחריותScraping responsibly
API קודם, גירוד רק כשאיןAPI first, scrape only when there's none
לאט, בלי מידע אישי, עם מטמוןSlowly, no personal data, with a cache
גירוד פוגש אנשים אמיתיים מהצד השני. כללי התנהגות: אם יש API, משתמשים בו. קוראים את robots.txt ואת תנאי השימוש של האתר. שולחים בקשה בקצב אנושי (שנייה ומעלה בין בקשות), שומרים במטמון כדי לא לבקש פעמיים, לא אוספים מידע אישי, ולא מפרסמים תוכן של אחרים כאילו הוא שלכם. פייתון אפילו יודעת לקרוא robots.txt בעצמה:Scraping meets real people on the other side. Etiquette: if there's an API, use it. Read the site's robots.txt and terms of use. Request at a human pace (a second or more between requests), cache so you never ask twice, don't collect personal data, and don't publish others' content as your own. Python can even read robots.txt by itself:
אנלוגיהAnalogy
לגרד אתר זה כמו להיכנס לספרייה ציבורית: מותר לקרוא ולרשום, אבל לא לסחוב את כל המדפים בבת אחת או להיכנס לחדר הצוות.Scraping a site is like visiting a public library: you may read and take notes, but not haul away every shelf at once or walk into the staff room.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
תגיות פותחות וסוגרות: <p>...</p>Opening and closing tags: <p>...</p>