נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

Web scraping אחראיResponsible web scraping

מפגש 26: HTML, BeautifulSoup, robots.txt, תנאי שימוש וקצב בקשות.Session 26: HTML, BeautifulSoup, robots.txt, terms of use and request rate.

מה נעשה היוםToday's plan

  • HTMLHTML
  • soupSoup
  • בורריםSelectors
  • הבאהFetching
  • עמודיםPages
  • אחריותResponsibility
נושאTopic 1

HTMLHTML

איך נראה דף מבפניםWhat a page looks like inside

  • תגיות פותחות וסוגרות: <p>...</p>Opening and closing tags: <p>...</p>
  • מאפיינים: class, hrefAttributes: class, href
  • בדפדפן: קליק ימני → InspectIn the browser: right-click → Inspect

אנלוגיה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.
נושאTopic 2

soupSoup

BeautifulSoupBeautifulSoup

  • BeautifulSoup(html, "html.parser")BeautifulSoup(html, "html.parser")
  • find = הראשון, find_all = כולםfind = the first, find_all = all
  • .get_text(strip=True) ו-tag["attr"].get_text(strip=True) and tag["attr"]

אנלוגיה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.
נושאTopic 3

בורריםSelectors

בוררי CSSCSS selectors

  • soup.select("article.book h3 a")soup.select("article.book h3 a")
  • .class, #id, רווח = בתוך.class, #id, a space = inside
  • select_one = הראשוןselect_one = the first

אנלוגיה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.
נושא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

אנלוגיה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.

טעות נפוצה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).
נושא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)

אנלוגיהAnalogy

לעבור עמודים זה כמו לדפדף בקטלוג: קוראים עמוד, מחפשים "המשך בעמוד הבא", ועוצרים כשאין יותר.Paging is like leafing through a catalogue: read a page, look for "continued on the next page", and stop when there's none.
נושאTopic 6

אחריותResponsibility

לגרד באחריותScraping responsibly

  • API קודם, גירוד רק כשאיןAPI first, scrape only when there's none
  • robots.txt ותנאי השימושrobots.txt and the terms of use
  • לאט, בלי מידע אישי, עם מטמוןSlowly, no personal data, with a cache

אנלוגיה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.

תגיות פותחות וסוגרות: <p>...</p>Opening and closing tags: <p>...</p>

מאפיינים: class, hrefAttributes: class, href

בדפדפן: קליק ימני → InspectIn the browser: right-click → Inspect

BeautifulSoup(html, "html.parser")BeautifulSoup(html, "html.parser")

find = הראשון, find_all = כולםfind = the first, find_all = all

.get_text(strip=True) ו-tag["attr"].get_text(strip=True) and tag["attr"]

soup.select("article.book h3 a")soup.select("article.book h3 a")

.class, #id, רווח = בתוך.class, #id, a space = inside

select_one = הראשוןselect_one = the first

requests.get(url, headers=...) ואז souprequests.get(url, headers=...) then soup

User-Agent שמזהה אתכםA User-Agent that identifies you

פונקציה אחת לדף אחדOne function per page

עוקבים אחרי קישור "next" עד שנגמרFollow the "next" link until it runs out

urljoin לכתובות יחסיותurljoin for relative addresses

שומרים ל-CSV (מפגש 21)Save to CSV (session 21)

API קודם, גירוד רק כשאיןAPI first, scrape only when there's none

robots.txt ותנאי השימושrobots.txt and the terms of use

מה למדנוWhat we learned

  • תגיות פותחות וסוגרות: <p>...</p>Opening and closing tags: <p>...</p>
  • BeautifulSoup(html, "html.parser")BeautifulSoup(html, "html.parser")
  • soup.select("article.book h3 a")soup.select("article.book h3 a")
  • requests.get(url, headers=...) ואז souprequests.get(url, headers=...) then soup
  • עוקבים אחרי קישור "next" עד שנגמרFollow the "next" link until it runs out
  • API קודם, גירוד רק כשאיןAPI first, scrape only when there's none

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich