Web Scraping & Bots · レッスン

CSS セレクターで DOM をたどる

BeautifulSoup で CSS セレクターを使い、基本的なタグ名検索を超えて、HTML の解析時に要素を正確に指定する方法を学びます。

レッスン 4/413 ステップ

「CSS セレクターで DOM をたどる」はCoddyKit上の無料Web Scraping & Botsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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.

無料で開始

AI チューターと学ぶ Python — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「CSS セレクターで DOM をたどる」レッスンは無料ですか?

はい。「CSS セレクターで DOM をたどる」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web Scraping & Botsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web Scraping & Botsコースには全4レッスンが含まれています。

「CSS セレクターで DOM をたどる」で何を学びますか?

BeautifulSoup で CSS セレクターを使い、基本的なタグ名検索を超えて、HTML の解析時に要素を正確に指定する方法を学びます。 ブラウザで直接実行するハンズオンコードでWeb Scraping & Botsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Web Scraping & Botsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWeb Scraping & Botsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「CSS セレクターで DOM をたどる」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWeb Scraping & Botsレッスンでコードを書いて実行できますか?

はい。すべてのWeb Scraping & Botsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 環境のセットアップ
  2. URLに対するRequestsの利用
  3. BeautifulSoupによるデータ抽出
  4. CSS セレクターで DOM をたどる
← Web Scraping & Botsに戻る