メインコンテンツへスキップ
PyPIパッケージ: olostep | 要件: Python 3.11+

インストール

pip install olostep

認証

Olostep DashboardからAPIキーを取得してください。

クイックスタート

SDKはユースケースに応じて2つのクライアントオプションを提供します:

同期クライアント (Olostep)

同期クライアント (Olostep) は、スクリプトやシンプルなユースケースに最適なブロッキングインターフェースを提供します。
from olostep import Olostep

# 'api_key' パラメータを渡すか、OLOSTEP_API_KEY 環境変数を設定してAPIキーを提供します

# 同期クライアントはリソース管理を自動的に処理します
# 明示的なクローズは不要 - 各操作後にリソースがクリーンアップされます
client = Olostep(api_key="YOUR_REAL_KEY")
scrape_result = client.scrapes.create(url_to_scrape="https://example.com")

基本的なウェブスクレイピング

from olostep import Olostep

client = Olostep(api_key="your-api-key")

# シンプルなスクレイピング
result = client.scrapes.create(url_to_scrape="https://example.com")
print(f"Scraped {len(result.html_content)} characters")

# 複数フォーマット
result = client.scrapes.create(
    url_to_scrape="https://example.com",
    formats=["html", "markdown"]
)
print(f"HTML: {len(result.html_content)} chars")
print(f"Markdown: {len(result.markdown_content)} chars")

バッチ処理

from olostep import Olostep

client = Olostep(api_key="your-api-key")

# 複数のURLを効率的に処理
batch = client.batches.create(
    urls=[
        "https://www.google.com/search?q=python",
        "https://www.google.com/search?q=javascript",
        "https://www.google.com/search?q=typescript"
    ]
)

# 完了を待って結果を処理
for item in batch.items():
    content = item.retrieve(["html"])
    print(f"Processed {item.url}: {len(content.html_content)} bytes")

スマートウェブクロール

from olostep import Olostep

client = Olostep(api_key="your-api-key")

# インテリジェントなフィルタリングでクロール
crawl = client.crawls.create(
    start_url="https://www.bbc.com",
    max_pages=100,
    include_urls=["/articles/**", "/blog/**"],
    exclude_urls=["/admin/**"]
)

for page in crawl.pages():
    content = page.retrieve(["html"])
    print(f"Crawled: {page.url}")

サイトマッピング

from olostep import Olostep

client = Olostep(api_key="your-api-key")

# ウェブサイトからすべてのリンクを抽出
maps = client.maps.create(url="https://example.com")

# 発見されたすべてのURLを取得
urls = []
for url in maps.urls():
    urls.append(url)
    if len(urls) >= 10:  # デモ用の制限
        break

print(f"Found {len(urls)} URLs")

AI駆動の回答

from olostep import Olostep

client = Olostep(api_key="your-api-key")

# AIを使用してウェブページから回答を取得
answer = client.answers.create(
    task="What is the main topic of https://example.com?"
)
print(f"Answer: {answer.answer}")

非同期クライアント (AsyncOlostep)

非同期クライアント (AsyncOlostep) は、高性能アプリケーション、バックエンドサービス、および多くの同時リクエストを処理する必要がある場合に推奨されるクライアントです。
from olostep import AsyncOlostep

# 'api_key' パラメータを渡すか、OLOSTEP_API_KEY 環境変数を設定してAPIキーを提供します

# リソース管理
# ===================
# SDKはリソース管理のために2つの使用パターンをサポートしています:

# 1. コンテキストマネージャ (一度限りの使用に推奨):
#    リソースのクリーンアップを自動的に処理
async with AsyncOlostep(api_key="YOUR_REAL_KEY") as client:
    scrape_result = await client.scrapes.create(url_to_scrape="https://example.com")
# トランスポートはここで自動的にクローズされます

# 2. 明示的なクローズ (長期間のサービス向け):
#    手動でのリソースクリーンアップが必要
client = AsyncOlostep(api_key="YOUR_REAL_KEY")
try:
    scrape_result = await client.scrapes.create(url_to_scrape="https://example.com")
finally:
    await client.close()  # トランスポートを手動でクローズ

基本的なウェブスクレイピング

import asyncio
from olostep import AsyncOlostep

async def main():
    async with AsyncOlostep(api_key="your-api-key") as client:
        # シンプルなスクレイピング
        result = await client.scrapes.create(url_to_scrape="https://example.com")
        print(f"Scraped {len(result.html_content)} characters")

        # 複数フォーマット
        result = await client.scrapes.create(
            url_to_scrape="https://example.com",
            formats=["html", "markdown"]
        )
        print(f"HTML: {len(result.html_content)} chars")
        print(f"Markdown: {len(result.markdown_content)} chars")

asyncio.run(main())

バッチ処理

import asyncio
from olostep import AsyncOlostep

async def main():
    async with AsyncOlostep(api_key="your-api-key") as client:
        # 複数のURLを効率的に処理
        batch = await client.batches.create(
            urls=[
                "https://www.google.com/search?q=python",
                "https://www.google.com/search?q=javascript",
                "https://www.google.com/search?q=typescript"
            ]
        )

        # 完了を待って結果を処理
        async for item in batch.items():
            content = await item.retrieve(["html"])
            print(f"Processed {item.url}: {len(content.html_content)} bytes")

asyncio.run(main())

スマートウェブクロール

import asyncio
from olostep import AsyncOlostep

async def main():
    async with AsyncOlostep(api_key="your-api-key") as client:
        # インテリジェントなフィルタリングでクロール
        crawl = await client.crawls.create(
            start_url="https://www.bbc.com",
            max_pages=100,
            include_urls=["/articles/**", "/blog/**"],
            exclude_urls=["/admin/**"]
        )

        async for page in crawl.pages():
            content = await page.retrieve(["html"])
            print(f"Crawled: {page.url}")

asyncio.run(main())

サイトマッピング

import asyncio
from olostep import AsyncOlostep

async def main():
    async with AsyncOlostep(api_key="your-api-key") as client:
        # ウェブサイトからすべてのリンクを抽出
        maps = await client.maps.create(url="https://example.com")

        # 発見されたすべてのURLを取得
        urls = []
        async for url in maps.urls():
            urls.append(url)
            if len(urls) >= 10:  # デモ用の制限
                break

        print(f"Found {len(urls)} URLs")

asyncio.run(main())

AI駆動の回答

import asyncio
from olostep import AsyncOlostep

async def main():
    async with AsyncOlostep(api_key="your-api-key") as client:
        # AIを使用してウェブページから回答を取得
        answer = await client.answers.create(
            task="What is the main topic of https://example.com?"
        )
        print(f"Answer: {answer.answer}")

asyncio.run(main())

SDKリファレンス

メソッド構造

両方のSDKクライアントは、論理的な名前空間に整理された同じクリーンでPython的なインターフェースを提供します:
名前空間目的主要メソッド
scrapes単一URLの抽出create(), get()
batches複数URLの処理create(), info(), items()
crawlsウェブサイトの巡回create(), info(), pages()
mapsリンク抽出create(), urls()
answersAI駆動の抽出create(), get()
retrieveコンテンツの取得get()
各操作は、後続の操作のためのエルゴノミックなメソッドを持つステートフルオブジェクトを返します。

エラーハンドリング

すべてのSDKエラーを基底例外クラスを使用してキャッチします:
from olostep import Olostep, Olostep_BaseError

client = Olostep(api_key="your-api-key")

try:
    result = client.scrapes.create(url_to_scrape="https://example.com")
except Olostep_BaseError as e:
    print(f"Error has occurred: {type(e).__name__}")
    print(f"Error message: {e}")
詳細なエラーハンドリング情報、完全な例外階層、および詳細なエラーハンドリングオプションについては、詳細なエラーハンドリングを参照してください。

自動リトライ

SDKは、RetryStrategy設定に基づいて、一時的なエラー(ネットワーク問題、一時的なサーバー問題)で自動的にリトライします。クライアントを作成する際にRetryStrategyインスタンスを渡すことで、リトライ動作をカスタマイズできます:
from olostep import Olostep, RetryStrategy

retry_strategy = RetryStrategy(
    max_retries=3,
    initial_delay=1.0,
    jitter_min=0.2,
    jitter_max=0.8
)

client = Olostep(api_key="your-api-key", retry_strategy=retry_strategy)
result = client.scrapes.create("https://example.com")
詳細なリトライ設定オプションとベストプラクティスについては、リトライ戦略を参照してください。

高度な機能

スマート入力強制

SDKは、最大限の利便性のためにさまざまな入力フォーマットをインテリジェントに処理します:
from olostep import Olostep, Country

client = Olostep(api_key="your-api-key")

# フォーマット: 文字列、リスト、または列挙型
client.scrapes.create(url_to_scrape="https://example.com", formats="html")
client.scrapes.create(url_to_scrape="https://example.com", formats=["html", "markdown"])

# 国: 大文字小文字を区別しない文字列または列挙型
client.scrapes.create(url_to_scrape="https://example.com", country="us")
client.scrapes.create(url_to_scrape="https://example.com", country=Country.US)

# リスト: 単一値またはリスト
client.batches.create(urls="https://example.com")    # 単一URL
client.batches.create(urls=["https://a.com", "https://b.com"])  # 複数URL

高度なスクレイピングオプション

from olostep import Olostep, Format, Country, WaitAction, FillInputAction

client = Olostep(api_key="your-api-key")

# スクレイピング動作を完全に制御
result = client.scrapes.create(
    url_to_scrape="https://news.google.com/",
    wait_before_scraping=3000,
    formats=[Format.HTML, Format.MARKDOWN],
    remove_css_selectors=["script", ".popup"],
    actions=[
        WaitAction(milliseconds=1500),
        FillInputAction(selector="searchbox", value="olostep")
    ],
    parser="@olostep/google-news",
    country=Country.US,
    remove_images=True
)

カスタムIDを使用したバッチ処理

from olostep import Olostep, Country

client = Olostep(api_key="your-api-key")

batch = client.batches.create([
    {"url": "https://www.google.com/search?q=python", "custom_id": "search_1"},
    {"url": "https://www.google.com/search?q=javascript", "custom_id": "search_2"},
    {"url": "https://www.google.com/search?q=typescript", "custom_id": "search_3"}
],
country=Country.US,
parser="@olostep/google-search"
)

# カスタムIDで結果を処理
# パーサーを使用する場合は、HTMLの代わりにJSONコンテンツを取得
for item in batch.items():
    if item.custom_id == "search_2":
        content = item.retrieve(["json"])
        print(f"Search result: {content.json_content}")

インテリジェントクロール

from olostep import Olostep

client = Olostep(api_key="your-api-key")

# インテリジェントなフィルタリングでクロール
crawl = client.crawls.create(
    start_url="https://www.bbc.com",
    max_pages=1000,
    max_depth=3,
    include_urls=["/articles/**", "/news/**"],
    exclude_urls=["/ads/**", "/tracking/**"],
    include_external=False,
    include_subdomain=True,
)

for page in crawl.pages():
    content = page.retrieve(["html"])
    print(f"Crawled: {page.url}")

フィルタ付きサイトマッピング

from olostep import Olostep

client = Olostep(api_key="your-api-key")

# 高度なフィルタリングでリンクを抽出
maps = client.maps.create(
    url="https://www.bbc.com",
    include_subdomain=True,
    include_urls=["/articles/**", "/news/**"],
    exclude_urls=["/ads/**", "/tracking/**"]
)

# フィルタされたURLを取得
urls = []
for url in maps.urls():
    urls.append(url)

print(f"Found {len(urls)} relevant URLs")

回答の取得

from olostep import Olostep

client = Olostep(api_key="your-api-key")

# まず回答を作成
created_answer = client.answers.create(
    task="What is the main topic of https://example.com?"
)

# 次にIDを使用して取得
answer = client.answers.get(answer_id=created_answer.id)
print(f"Answer: {answer.answer}")

コンテンツの取得

from olostep import Olostep

client = Olostep(api_key="your-api-key")

# 取得IDでコンテンツを取得
result = client.retrieve.get(retrieve_id="ret_123")

# 複数フォーマットを取得
result = client.retrieve.get(retrieve_id="ret_123", formats=["html", "markdown", "text", "json"])

ロギング

問題をデバッグするためにロギングを有効にします:
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("olostep")
logger.setLevel(logging.INFO)  # 詳細な出力にはDEBUGを使用
ログレベル: INFO (推奨), DEBUG (詳細), WARNING, ERROR

リトライ戦略の設定

RetryStrategyクラスは、オロステップSDKが一時的なAPIエラーをどのように処理するかを制御します。自動リトライと指数バックオフおよびジッターを使用して、プロダクション環境での信頼性の高い操作を確保します。これにより、一時的なネットワーク問題、レート制限、サーバーの過負荷による断続的な失敗を防ぎます。

デフォルトの動作

デフォルトで、SDKは以下のリトライ設定を使用します:
  • 最大リトライ回数: 5回の試行
  • 初期遅延: 2秒
  • バックオフ: 指数関数 (2^試行)
  • ジッター: 遅延の10-90% (ランダム化)
これは次のことを意味します:
  • 試行1: 即時
  • 試行2: 約2-3.6秒の遅延
  • 試行3: 約4-7.2秒の遅延
  • 試行4: 約8-14.4秒の遅延
  • 試行5: 約16-28.8秒の遅延
最大持続時間: 約57秒(最悪の場合)

カスタム設定

from olostep import AsyncOlostep, RetryStrategy

# カスタムリトライ戦略を作成
retry_strategy = RetryStrategy(
    max_retries=3,
    initial_delay=1.0,
    jitter_min=0.2,  # 最小ジッター20%
    jitter_max=0.8,  # 最大ジッター80%
)

# クライアントで使用
async with AsyncOlostep(
    api_key="your-api-key",
    retry_strategy=retry_strategy
) as client:
    result = await client.scrapes.create("https://example.com")

リトライが発生する場合

SDKは以下の場合に自動的にリトライします:
  • 一時的なサーバーの問題 (OlostepServerError_TemporaryIssue)
  • タイムアウト応答 (OlostepServerError_NoResultInResponse)
その他のエラー(認証、検証、リソースが見つからないなど)は、リトライなしで即座に失敗します。

トランスポートと呼び出し元のリトライ

SDKには2つのリトライレイヤーがあります:
  1. トランスポートレイヤー: ネットワークレベルの接続失敗を処理(DNS、タイムアウトなど)
  2. 呼び出し元レイヤー: APIレベルの一時的なエラーを処理(RetryStrategyによって制御)
両方のレイヤーは独立しており、別々に設定できます。最大持続時間は両方のレイヤーの合計です。

最大持続時間の計算

retry_strategy = RetryStrategy(max_retries=5, initial_delay=2.0)
max_duration = retry_strategy.max_duration()
print(f"Max call duration: {max_duration:.2f}s")

設定例

異なるユースケースに対するリトライ戦略の設定例を以下に示します。

保守的な戦略

# リトライ回数を減らし、遅延を短く
retry_strategy = RetryStrategy(
    max_retries=3,
    initial_delay=1.0,
    jitter_min=0.2,
    jitter_max=0.8
)
# 最大持続時間: 約12.6秒

積極的な戦略

# 重要な操作のためにリトライ回数を増やす
retry_strategy = RetryStrategy(
    max_retries=10,
    initial_delay=0.5
)
# 最大持続時間: 約969.75秒

リトライなし(即時失敗)

# 即時の失敗フィードバックのためにリトライを無効にする
retry_strategy = RetryStrategy(max_retries=0)

client = AsyncOlostep(api_key="your-api-key", retry_strategy=retry_strategy)

高スループット戦略

# 高ボリューム操作に最適化
retry_strategy = RetryStrategy(
    max_retries=2,
    initial_delay=0.5,
    jitter_min=0.1,
    jitter_max=0.3  # より予測可能なタイミングのためにジッターを低くする
)
# 最大持続時間: 約1.95秒

ジッターの理解

ジッターは、クライアントが同時にリトライする際の「サンダリングハード」問題を防ぐためにランダム化を追加します。ジッターは次のように計算されます:
base_delay = initial_delay * (2 ** attempt)
jitter_range = base_delay * (jitter_max - jitter_min)
jitter = random.uniform(base_delay * jitter_min, base_delay * jitter_min + jitter_range)
final_delay = base_delay + jitter
例として、initial_delay=2.0, jitter_min=0.1, jitter_max=0.9の場合:
  • 試行0: base=2.0秒, jitter=0.2-1.8秒, final=2.2-3.8秒
  • 試行1: base=4.0秒, jitter=0.4-3.6秒, final=4.4-7.6秒
  • 試行2: base=8.0秒, jitter=0.8-7.2秒, final=8.8-15.2秒

ベストプラクティス

本番アプリケーション向け

# 本番用のバランスの取れたアプローチ
retry_strategy = RetryStrategy(
    max_retries=5,
    initial_delay=2.0,
    jitter_min=0.1,
    jitter_max=0.9
)

開発/テスト向け

# 開発用の迅速なフィードバック
retry_strategy = RetryStrategy(
    max_retries=2,
    initial_delay=0.5,
    jitter_min=0.1,
    jitter_max=0.3
)

バッチ操作向け

# 大規模バッチジョブ用の保守的なアプローチ
retry_strategy = RetryStrategy(
    max_retries=3,
    initial_delay=1.0,
    jitter_min=0.2,
    jitter_max=0.8
)

モニタリングとデバッグ

SDKはDEBUGレベルでリトライ情報をログに記録します:
DEBUG: Temporary issue, retrying in 2.34s
DEBUG: No result in response, retrying in 4.67s
リトライ動作を監視するためにデバッグロギングを有効にします:
import logging
logging.getLogger("olostep").setLevel(logging.DEBUG)

エラーハンドリング

すべてのリトライが尽きた場合、元のエラーが発生します:
try:
    result = await client.scrapes.create("https://example.com")
except OlostepServerError_TemporaryIssue as e:
    print(f"Failed after all retries: {e}")
    # 永続的な失敗を処理

パフォーマンスの考慮事項

  • メモリ: 各リトライ試行は、リクエスト/レスポンスオブジェクトに追加のメモリを使用します
  • 時間: リトライが有効な場合、全体の操作時間が大幅に長くなる可能性があります
  • API制限: リトライはAPI使用制限にカウントされます
  • ネットワーク: リトライ試行によるネットワークトラフィックの増加
信頼性とパフォーマンスの要件に基づいて、リトライ戦略を選択してください。

詳細なエラーハンドリング

例外階層

Olostep SDKは、さまざまな失敗シナリオに対する包括的な例外階層を提供します。すべての例外はOlostep_BaseErrorから継承されます。 Olostep_BaseErrorから直接継承される3つの主要なエラータイプがあります:
  1. Olostep_APIConnectionError - ネットワークレベルの接続失敗
  2. OlostepServerError_BaseError - APIサーバーによって(ある程度)発生するエラー
  3. OlostepClientError_BaseError - クライアントSDKによって発生するエラー

なぜ接続エラーが別なのか

Olostep_APIConnectionErrorは、サーバーエラーとは別です。これは、APIがリクエストを処理する前に発生するネットワークレベルの失敗を表します。これらはトランスポートレイヤーの問題(DNSまたはHTTPの失敗、タイムアウト、接続拒否など)であり、APIレベルのエラーではありません。HTTPステータスコード(4xx、5xx)はAPI応答と見なされ、サーバーエラーとして分類されますが、問題を示しています。
Olostep_BaseError
├── Olostep_APIConnectionError
├── OlostepServerError_BaseError
│   ├── OlostepServerError_TemporaryIssue
│   │   ├── OlostepServerError_NetworkBusy
│   │   └── OlostepServerError_InternalNetworkIssue
│   ├── OlostepServerError_RequestUnprocessable
│   │   ├── OlostepServerError_ParserNotFound
│   │   └── OlostepServerError_OutOfResources
│   ├── OlostepServerError_BlacklistedDomain
│   ├── OlostepServerError_FeatureApprovalRequired
│   ├── OlostepServerError_AuthFailed
│   ├── OlostepServerError_CreditsExhausted
│   ├── OlostepServerError_InvalidEndpointCalled
│   ├── OlostepServerError_ResourceNotFound
│   ├── OlostepServerError_NoResultInResponse
│   └── OlostepServerError_UnknownIssue
└── OlostepClientError_BaseError
    ├── OlostepClientError_RequestValidationFailed
    ├── OlostepClientError_ResponseValidationFailed
    ├── OlostepClientError_NoAPIKey
    ├── OlostepClientError_AsyncContext
    ├── OlostepClientError_BetaFeatureAccessRequired
    └── OlostepClientError_Timeout

推奨されるエラーハンドリング

ほとんどのユースケースでは、基底エラーをキャッチしてエラー名を出力します:
from olostep import AsyncOlostep, Olostep_BaseError

try:
    result = await client.scrapes.create(url_to_scrape="https://example.com")
except Olostep_BaseError as e:
    print(f"Error has occurred: {type(e).__name__}")
    print(f"Error message: {e}")
このアプローチは、すべてのSDKエラーをキャッチし、何が問題だったのかを明確に示します。エラー名(例:OlostepServerError_AuthFailed)は、問題を理解するのに十分な説明を提供します。

詳細なエラーハンドリング

より具体的なエラーハンドリングが必要な場合は、特定のエラータイプを直接キャッチします。OlostepServerError_BaseErrorまたはOlostepClientError_BaseErrorを使用しないでください - これらの基底クラスは、エラーを引き起こしたのがサーバーかクライアントかを示すだけで、誰が修正する責任があるかを示しません。これは、エラーハンドリングロジックに役立たない実装の詳細です。 代わりに、実際の問題を示す特定のエラータイプをキャッチします:
from olostep import (
    AsyncOlostep,
    Olostep_BaseError,
    Olostep_APIConnectionError,
    OlostepServerError_AuthFailed,
    OlostepServerError_CreditsExhausted,
    OlostepClientError_NoAPIKey,
)

try:
    result = await client.scrapes.create(url_to_scrape="https://example.com")
except Olostep_APIConnectionError as e:
    print(f"Network error: {type(e).__name__}")
except OlostepServerError_AuthFailed:
    print("Invalid API key")
except OlostepServerError_CreditsExhausted:
    print("Credits exhausted")
except OlostepClientError_NoAPIKey:
    print("API key not provided")
except Olostep_BaseError as e:
    print(f"Error has occurred: {type(e).__name__}")

設定

環境変数

変数説明デフォルト
OLOSTEP_API_KEYあなたのAPIキー必須
OLOSTEP_BASE_API_URLAPIベースURLhttps://api.olostep.com/v1
OLOSTEP_API_TIMEOUTリクエストタイムアウト(秒)150

ヘルプを得る

リソース