ตัวเลือก CSS เพื่อความแม่นยำ
ใช้ตัวเลือก CSS เพื่อระบุและดึงข้อมูลจากองค์ประกอบตามรูปแบบและแอตทริบิวต์ขององค์ประกอบเหล่านั้น
ตัวเลือก CSS เพื่อความแม่นยำ เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Scraping & Bots และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What are CSS Selectors?
CSS selectors are patterns used to select elements on a web page. Think of them as precise instructions for finding specific pieces of information.
Web browsers use them to apply styles (CSS), and we'll use them to extract data efficiently.
Selectors for Data Extraction
In web scraping, CSS selectors provide a powerful way to pinpoint exactly the data you need from complex HTML.
- They are often more concise than XPath.
- Many developers are already familiar with CSS.
- BeautifulSoup has excellent support for them.
Selecting by Tag Name
The simplest selector is the tag name. This selects all elements of that type.
For example, p selects all paragraph tags, and a selects all anchor (link) tags.
Example: To find all list items, you'd use li.
from bs4 import BeautifulSoup
html_doc = """
<html><body>
<h1>My Title</h1>
<p>First paragraph.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</body></html>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
# Select all 'p' tags
paragraphs = soup.select('p')
for p in paragraphs:
print(p.get_text())
Class and ID Selectors
You can select elements based on their class or ID attributes. These are very common for styling and unique identification.
- Class: Use a dot
.before the class name (e.g.,.product-title). - ID: Use a hash
#before the ID name (e.g.,#main-content). IDs should be unique!
from bs4 import BeautifulSoup
html_doc = """
<div id="header">Welcome</div>
<p class="intro">Hello there!</p>
<p class="intro">Another intro.</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
header = soup.select_one('#header')
print("Header:", header.get_text())
intros = soup.select('.intro')
for p in intros:
print("Intro:", p.get_text())
Selecting Nested Elements
To select elements that are inside other elements, you use a space between selectors. This is called a descendant selector.
It means "find an element (B) that is anywhere inside another element (A)".
Example: div p selects all <p> tags that are inside any <div> tag.
from bs4 import BeautifulSoup
html_doc = """
<div>
<p>Inside div paragraph 1</p>
<span>
<p>Inside span inside div</p>
</span>
</div>
<p>Outside div paragraph</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
div_paragraphs = soup.select('div p')
for p in div_paragraphs:
print(p.get_text())
Direct Children Only
Sometimes you only want elements that are direct children of another element, not just any descendant.
Use the greater than symbol > for this.
Example: ul > li selects all <li> tags that are direct children of a <ul> tag.
from bs4 import BeautifulSoup
html_doc = """
<div class="container">
<p>Direct child P</p>
<div>
<p>Nested P (not direct)</p>
</div>
</div>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
direct_p = soup.select('.container > p')
for p in direct_p:
print(p.get_text())
Selecting by Attributes
You can select elements based on their attributes and even their attribute values!
[attr]: Has the attribute (e.g.,[href]).[attr="value"]: Has attribute with exact value (e.g.,[target="_blank"]).[attr^="value"]: Attribute value starts with (e.g.,[src^="data:"]).[attr$="value"]: Attribute value ends with (e.g.,[alt$="logo"]).[attr*="value"]: Attribute value contains (e.g.,[id*="item"]).
from bs4 import BeautifulSoup
html_doc = """
<a href="/about">About Us</a>
<a href="https://example.com/contact" target="_blank">Contact</a>
<img src="image.jpg" alt="product image">
"""
soup = BeautifulSoup(html_doc, 'html.parser')
# Select links with target="_blank"
external_links = soup.select('a[target="_blank"]')
for link in external_links:
print("External:", link.get('href'))
# Select images with alt containing "image"
product_images = soup.select('img[alt*="image"]')
for img in product_images:
print("Image:", img.get('src'))
Combining with Commas
To select elements that match any of several different selectors, you can separate them with a comma ,.
This is useful when you want to gather data from different types of elements or locations.
Example: h1, h2, h3 selects all heading tags of level 1, 2, or 3.
from bs4 import BeautifulSoup
html_doc = """
<h1>Main Heading</h1>
<p>Some text.</p>
<h2>Sub Heading</h2>
<div>Another div.</div>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
headings = soup.select('h1, h2')
for h in headings:
print(h.get_text())
Pseudo-classes for Position
CSS pseudo-classes allow selection based on state or position, not just attributes. For scraping, position-based ones are very useful.
:first-child: Selects the first child element.:last-child: Selects the last child element.:nth-of-type(n): Selects the Nth element of a specific type (e.g.,li:nth-of-type(2)for the second list item).
from bs4 import BeautifulSoup
html_doc = """
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ul>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
first_item = soup.select_one('li:first-child')
print("First:", first_item.get_text())
second_item = soup.select_one('li:nth-of-type(2)')
print("Second:", second_item.get_text())
Practical CSS Selector Use
Let's combine what we've learned to extract specific data from a sample product listing.
We want the title and price of the first product.
from bs4 import BeautifulSoup
html_doc = """
<div class="product-list">
<div class="product-card">
<h3 class="product-title">Laptop X1</h3>
<p class="product-price">$999.99</p>
<button class="add-to-cart">Add</button>
</div>
<div class="product-card">
<h3 class="product-title">Mouse Z2</h3>
<p class="product-price">$29.99</p>
<button class="add-to-cart">Add</button>
</div>
</div>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
# Select the first product card
first_product = soup.select_one('.product-card:first-of-type')
if first_product:
title = first_product.select_one('.product-title')
price = first_product.select_one('.product-price')
print("Title:", title.get_text())
print("Price:", price.get_text())
else:
print("No product found.")
Quick Check on Selectors
Given the HTML below, what CSS selector would correctly select the text "Product Name 2"?
<div class="items">
<div id="item-1">
<span class="name">Product Name 1</span>
</div>
<div id="item-2">
<span class="name">Product Name 2</span>
</div>
<p class="name">Other Name</p>
</div>Recap: CSS Selectors
Great job! You've mastered the basics of CSS selectors for web scraping.
- We learned to select by tag, class, and ID.
- We explored descendant (space) and direct child (
>) selectors. - You can filter by attributes (
[attr="value"]) and use pseudo-classes like:first-child. - BeautifulSoup's
.select()and.select_one()methods make using them easy in Python.
Next, we'll look into XPath for even more powerful selections!
คำถามที่พบบ่อย
บทเรียน “ตัวเลือก CSS เพื่อความแม่นยำ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวเลือก CSS เพื่อความแม่นยำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวเลือก CSS เพื่อความแม่นยำ”
ใช้ตัวเลือก CSS เพื่อระบุและดึงข้อมูลจากองค์ประกอบตามรูปแบบและแอตทริบิวต์ขององค์ประกอบเหล่านั้น คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวเลือก CSS เพื่อความแม่นยำ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Scraping & Bots นี้ได้ไหม
ได้ บทเรียน Web Scraping & Bots ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การนำทางโครงสร้าง HTML ที่ซับซ้อน
- ตัวเลือก CSS เพื่อความแม่นยำ
- XPath สำหรับการเลือกที่มีประสิทธิภาพ
- การดึงข้อมูลจากตาราง HTML