การนำทาง DOM ด้วยตัวเลือก CSS
ใช้ตัวเลือก CSS ร่วมกับ BeautifulSoup เพื่อระบุองค์ประกอบที่ต้องการได้อย่างแม่นยำเมื่อแยกวิเคราะห์ HTML โดยก้าวไปไกลกว่าการค้นหาด้วยชื่อแท็กพื้นฐาน
การนำทาง DOM ด้วยตัวเลือก CSS เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Scraping & Bots และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why CSS Selectors?
You can find tags by name with BeautifulSoup, but real pages need precision. CSS selectors let you target elements exactly like a stylesheet would.
select vs find
BeautifulSoup offers select() (returns a list) and select_one() (returns the first match) that accept CSS selector strings.
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
items = soup.select('li')
first = soup.select_one('h1')Selecting by Class
Prefix a class name with a dot.
prices = soup.select('.price')
for p in prices:
print(p.get_text())Selecting by ID
Prefix an id with a hash. IDs are unique, so pair with select_one.
header = soup.select_one('#main-header')
print(header.get_text())Descendant Selectors
A space means descendant: match elements nested anywhere inside another.
titles = soup.select('div.product h2')
for t in titles:
print(t.get_text(strip=True))Direct Child Selectors
A > matches only direct children, not deeper descendants.
rows = soup.select('table > tbody > tr')
print(len(rows))Attribute Selectors
Target elements by attribute values inside square brackets.
links = soup.select('a[href^="https"]')
images = soup.select('img[alt]')Combining Selectors
Chain conditions for tight targeting — tag, class and attribute together.
buttons = soup.select('button.cta[data-action="buy"]')Extracting Attributes
Once selected, read attributes like href or src with bracket access.
for a in soup.select('a.product-link'):
print(a['href'])nth-of-type
Pseudo-classes like :nth-of-type pick elements by position.
second = soup.select('ul li:nth-of-type(2)')
print(second[0].get_text())A Full Mini Example
Combine everything to scrape a list of product names and prices.
for card in soup.select('div.card'):
name = card.select_one('.title').get_text(strip=True)
price = card.select_one('.price').get_text(strip=True)
print(name, price)Quick Check
Which CSS selector matches an element whose class is exactly price?
Recap
You learned CSS selectors in BeautifulSoup:
- select / select_one
- Class (.), id (#), descendant and child (>) selectors
- Attribute selectors and pseudo-classes
- Extracting attributes
CSS selectors let you target exactly the data you need on complex pages.
คำถามที่พบบ่อย
บทเรียน “การนำทาง DOM ด้วยตัวเลือก CSS” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การนำทาง DOM ด้วยตัวเลือก CSS” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การนำทาง DOM ด้วยตัวเลือก CSS”
ใช้ตัวเลือก CSS ร่วมกับ BeautifulSoup เพื่อระบุองค์ประกอบที่ต้องการได้อย่างแม่นยำเมื่อแยกวิเคราะห์ HTML โดยก้าวไปไกลกว่าการค้นหาด้วยชื่อแท็กพื้นฐาน คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การนำทาง DOM ด้วยตัวเลือก CSS” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Scraping & Bots นี้ได้ไหม
ได้ บทเรียน Web Scraping & Bots ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่าสภาพแวดล้อม
- การใช้ Requests กับ URL
- การดึงข้อมูลด้วย BeautifulSoup
- การนำทาง DOM ด้วยตัวเลือก CSS