0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ScraplingでWebスクレイピング完全ガイド2025

Last updated at Posted at 2025-11-25

Scraplingは、モジュール式のインストール方法を採用しています 🔧:

基本インストール(パーサーのみ、フェッチャーなし):

pip install scrapling

フェッチャーとブラウザ依存関係を含むインストール:

pip install "scrapling[fetchers]"
scrapling install

scrapling installコマンドは、システム依存関係とフィンガープリント操作ツールを含むすべてのブラウザをダウンロードします。

すべてをインストール(フェッチャー、AI機能、CLIツール):

pip install "scrapling[all]"
scrapling install

ステップ1:ウェブページからHTMLを取得する

まず、Scraplingがターゲットサイトにアクセスできるかテストしましょう。HTTPリクエストにはFetcherクラスを使用します 📝:

from scrapling.fetchers import Fetcher

target_page = Fetcher.get("https://www.scrapingcourse.com/ecommerce/")
print(target_page.status)  # Should print: 200
print(target_page.html_content)

成功すれば、HTTPステータスコード200とウェブページの完全なHTMLが表示されます。

ステップ2:CSSセレクタを使用して商品データを抽出する

Scraplingはアダプティブセレクタを使用して、軽微なレイアウト変更に自動的に適応できますが、この機能はデフォルトで無効になっています。

まず、DevToolsを使って商品要素を検査します。このサイトの場合:

  • 商品名はh2.woocommerce-loop-product__titleにあります
  • 価格は.priceにあります
  • 画像は.woocommerce-LoopProduct-link imgにあります
from scrapling.fetchers import Fetcher

target_page = Fetcher.get("https://www.scrapingcourse.com/ecommerce/")

## 商品データを抽出
product_names = target_page.css("h2.woocommerce-loop-product__title")
product_prices = target_page.css(".price")
product_images = target_page.css(".woocommerce-LoopProduct-link img")

extracted_products = []
for product_name, product_price, product_image in zip(product_names, product_prices, product_images):
    # 正規表現を使って価格の数値だけを抽出
    numeric_price = product_price.re_first(r'[\d.,]+')
    
    product_info = {
        "name": product_name.text,
        "price": f"${numeric_price}",
        "image": product_image.attrib["src"],
    }
    extracted_products.append(product_info)

print(extracted_products)

アダプティブセレクタを有効にする方法(サイト更新後も要素を追跡):

## グローバルにアダプティブを有効化
Fetcher.adaptive = True
target_page = Fetcher.get("https://www.scrapingcourse.com/ecommerce/")

## 将来の適応のために要素プロパティを保存
product_names = target_page.css(".product-name", auto_save=True)

## 後でサイト構造が変わったときは、アダプティブモードを使用
product_names = target_page.css(".product-name", adaptive=True)  # CSSが変わってもScraplingが見つけます!

ステップ3:ステルスモードを有効にする 🕵️

多くのウェブサイトは、Cloudflareなどのアンチボットツールを使用してスクレイパーをブロックします。ScraplingのStealthyFetcherは、高度なフィンガープリント偽装機能を備えた改良版Firefoxブラウザを使用します。

StealthySessionまたはStealthyFetcherheadless=Trueフラグとともに使用します:

from scrapling.fetchers import StealthyFetcher

## 一回限りのリクエスト(ブラウザを開いて閉じる)
stealth_page = StealthyFetcher.fetch(
    "https://www.scrapingcourse.com/cloudflare-challenge/",
    headless=True
)
print(stealth_page.status)

複数のリクエストの場合、セッションを使ってブラウザを開いたままにします:

from scrapling.fetchers import StealthySession

## 複数のリクエストのためにブラウザを開いたままにする
with StealthySession(headless=True) as session:
    stealth_page = session.fetch("https://www.scrapingcourse.com/cloudflare-challenge/")
    print(stealth_page.html_content)

注意: solve_cloudflareパラメータは利用可能ですが、特定のCloudflare保護タイプに基づいて慎重に使用する必要があります。

実世界の例:ECサイトデータのスクレイピング 🚀

エラーハンドリングを含む完全なスクレイパーの例:

from scrapling.fetchers import Fetcher

target_page = Fetcher.get("https://www.scrapingcourse.com/ecommerce/")

if target_page.status != 200:
    print(f"ページの取得に失敗しました: {target_page.status}")
    exit()

product_names = target_page.css("h2.woocommerce-loop-product__title")
product_prices = target_page.css(".price")
product_images = target_page.css(".woocommerce-LoopProduct-link img")

extracted_products = []
for product_name, product_price, product_image in zip(product_names, product_prices, product_images):
    numeric_price = product_price.re_first(r'[\d.,]+')
    
    product_info = {
        "name": product_name.text,
        "price": f"${numeric_price}",
        "image": product_image.attrib["src"],
    }
    extracted_products.append(product_info)

for single_product in extracted_products:
    print(single_product)

出力構造:

[
    {"name": "Abominable Hoodie", "price": "$69.00", "image": "https://...jpg"},
    {"name": "Artemis Running Short", "price": "$45.00", "image": "https://...jpg"}
]

Scraplingの制限事項 ⚠️

Scraplingは小規模から中規模のスクレイピングに適していますが、いくつかの壁にぶつかります:

  • プロキシローテーションの組み込みサポートなし: プロキシのローテーションや自動ジオターゲティングのネイティブサポートがありません
  • ブラウザベースのスクレイピングはリソースを消費: DynamicFetcherStealthyFetcherはブラウザインスタンスを使用するため、大量のメモリを消費します
  • アダプティブ機能は手動で有効化が必要: アダプティブ機能はデフォルトで無効で、明示的な設定が必要です
  • スケーリングインフラストラクチャなし: 並行処理、リトライ、分散スクレイピングを自分で管理する必要があります
  • アダプティブは最初の要素のみ: アダプティブデータを保存する際、最初の要素のプロパティのみが保存されます

スケーリングのヒント:プロキシ管理にBright Dataを使用する

Scraplingのプロキシ制限を克服するには、Bright DataやOxylabsなどのサービスと統合します。これらのプロバイダーは通常、レジデンシャルプロキシ、ローテーションIP、ジオターゲティングを提供しています。

FetcherSessionの使用例:

from scrapling.fetchers import FetcherSession

proxy_config = {
    "http": "http://username:password@brd.superproxy.io:22225",
    "https": "http://username:password@brd.superproxy.io:22225",
}

with FetcherSession() as session:
    proxy_page = session.get(
        "https://www.scrapingcourse.com/ecommerce/",
        proxies=proxy_config
    )
    print(proxy_page.html_content)

適切な認証情報を持つアカウントが必要です。この統合により、IPバンを回避し、より大量のスクレイピングを行うことができます。

Scraplingのベストプラクティス 💡

必要に応じてアダプティブセレクタを有効にする: サイト更新で壊れる可能性のある重要なセレクタにはauto_save=Trueを使用します

適切なフェッチャーを選択する:

フェッチャータイプ 用途 特徴
Fetcher 静的サイト 最速、シンプルなHTMLページに最適
DynamicFetcher JavaScript多用サイト 基本的な保護を持つ動的コンテンツに対応
StealthyFetcher 高度なアンチボットシステム フィンガープリント偽装で検出を回避

複数のリクエストにはセッションを使用: セッションクラスでブラウザインスタンスを再利用してオーバーヘッドを削減します

プロキシをローテーション: 大規模操作でIPブロックを防ぎます

レート制限を追加: time.sleep()またはキューシステムを使用してリクエストを制限します

組み込み正規表現を活用: 正確なデータ抽出には.re()および.re_first()メソッドを使用します

エラーを適切に処理: 常にレスポンスステータスをチェックし、例外を処理します

並行処理には非同期を使用: 並列リクエストにはAsyncFetcherと非同期セッションを活用します

まとめ

Scraplingは、2025年においてスクレイパーを構築するためのよりクリーンで回復力のある方法を提供します。そのアダプティブセレクタと複数のフェッチャーオプションにより、シンプルな静的ページから高度なアンチボット保護を持つサイトまで、幅広いサイトに対応できます。ただし、完璧ではありません — 大規模な運用には、プロキシローテーションや分散システムなどの外部インフラストラクチャが依然として必要です。

ScraplingをBright Dataまたはその他の高品質なローテーションプロキシと組み合わせれば、継続的なメンテナンスなしでほとんどのスクレイピング課題に対応できる強力なセットアップが手に入ります。このライブラリのモジュール設計(v0.3.2以降)により、必要なものだけをインストールでき、92%のテストカバレッジが信頼性を保証します。

質問があればコメントで教えてください!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?