> ## Documentation Index
> Fetch the complete documentation index at: https://docs.olostep.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Olostep + LangChain 統合

> LangChainを使用して、ウェブスクレイピングと検索機能を持つインテリジェントなAIエージェントを構築

Olostep LangChain統合は、あらゆるウェブサイトからデータを検索、スクレイピング、分析、構造化できるAIエージェントを構築するための包括的なツールを提供します。LangChainおよびLangGraphアプリケーションに最適です。

## 特徴

この統合は、Olostep APIのすべての5つの機能にアクセスできます：

<CardGroup cols={2}>
  <Card title="スクレイプ" icon="file-lines">
    任意の単一URLから複数の形式（Markdown、HTML、JSON、テキスト）でコンテンツを抽出
  </Card>

  <Card title="バッチ" icon="layer-group">
    最大10,000のURLを並行して処理。バッチジョブは5-8分で完了
  </Card>

  <Card title="回答" icon="question">
    自然言語クエリと構造化出力を備えたAI駆動のウェブ検索
  </Card>

  <Card title="マップ" icon="map">
    サイト構造分析のためにウェブサイトからすべてのURLを抽出
  </Card>

  <Card title="クロール" icon="spider-web">
    リンクをたどってウェブサイト全体を自動的に発見しスクレイプ
  </Card>
</CardGroup>

## インストール

<CodeGroup>
  ```bash pip theme={null}
  pip install langchain-olostep
  ```

  ```bash poetry theme={null}
  poetry add langchain-olostep
  ```
</CodeGroup>

## セットアップ

Olostep APIキーを環境変数として設定します：

```bash theme={null}
export OLOSTEP_API_KEY="your_olostep_api_key_here"
```

APIキーは[Olostep Dashboard](https://olostep.com/dashboard)から取得してください。

## 利用可能なツール

### scrape\_website

単一のURLからコンテンツを抽出します。複数の形式とJavaScriptレンダリングをサポート。

<ParamField path="url" type="string" required>
  スクレイプするウェブサイトのURL（[http://またはhttps://を含む必要があります）](http://またはhttps://を含む必要があります）)
</ParamField>

<ParamField path="format" type="string" default="markdown">
  出力形式：`markdown`、`html`、`json`、または`text`
</ParamField>

<ParamField path="country" type="string">
  ロケーション固有のコンテンツのための国コード（例："US"、"GB"、"CA"）
</ParamField>

<ParamField path="wait_before_scraping" type="integer">
  JavaScriptレンダリングの待機時間（ミリ秒単位、0-10000）
</ParamField>

<ParamField path="parser" type="string">
  特殊な抽出のためのオプションのパーサーID（例："@olostep/amazon-product"）
</ParamField>

<CodeGroup>
  ```python Basic Scraping theme={null}
  from langchain_olostep import scrape_website
  import asyncio

  # ウェブサイトをスクレイプ
  content = asyncio.run(scrape_website.ainvoke({
      "url": "https://example.com",
      "format": "markdown"
  }))

  print(content)
  ```

  ```python With JavaScript theme={null}
  # 動的コンテンツを待機
  content = asyncio.run(scrape_website.ainvoke({
      "url": "https://example.com",
      "format": "markdown",
      "wait_before_scraping": 2000
  }))
  ```

  ```python With Parser theme={null}
  # 特殊なパーサーを使用
  content = asyncio.run(scrape_website.ainvoke({
      "url": "https://www.amazon.com/dp/PRODUCT_ID",
      "parser": "@olostep/amazon-product",
      "format": "json"
  }))
  ```
</CodeGroup>

### scrape\_batch

複数のURLを並行して処理（最大10,000件まで一度に）。

<ParamField path="urls" type="array" required>
  スクレイプするURLのリスト
</ParamField>

<ParamField path="format" type="string" default="markdown">
  すべてのURLの出力形式：`markdown`、`html`、`json`、または`text`
</ParamField>

<ParamField path="country" type="string">
  ロケーション固有のコンテンツのための国コード
</ParamField>

<ParamField path="wait_before_scraping" type="integer">
  JavaScriptレンダリングの待機時間
</ParamField>

<ParamField path="parser" type="string">
  特殊な抽出のためのオプションのパーサーID
</ParamField>

<CodeGroup>
  ```python Batch Scraping theme={null}
  from langchain_olostep import scrape_batch
  import asyncio

  # 複数のURLをスクレイプ
  result = asyncio.run(scrape_batch.ainvoke({
      "urls": [
          "https://example1.com",
          "https://example2.com",
          "https://example3.com"
      ],
      "format": "markdown"
  }))

  print(result)
  # 戻り値: {"batch_id": "batch_xxx", "status": "in_progress", ...}
  ```
</CodeGroup>

### answer\_question

ウェブを検索し、ソース付きのAI駆動の回答を取得。データ強化や調査に最適。

<ParamField path="task" type="string" required>
  検索する質問またはタスク
</ParamField>

<ParamField path="json_schema" type="object">
  望ましい出力形式を記述するオプションのJSONスキーマ辞書/文字列
</ParamField>

<CodeGroup>
  ```python Simple Question theme={null}
  from langchain_olostep import answer_question
  import asyncio

  # 簡単な質問をする
  result = asyncio.run(answer_question.ainvoke({
      "task": "What is the capital of France?"
  }))

  print(result)
  # 戻り値: {"answer": {"result": "Paris"}, "sources": [...]}
  ```

  ```python Structured Output theme={null}
  # JSONスキーマで構造化データを取得
  result = asyncio.run(answer_question.ainvoke({
      "task": "What is the latest book by J.K. Rowling?",
      "json_schema": {
          "book_title": "",
          "author": "",
          "release_date": ""
      }
  }))

  print(result)
  # ソース付きの構造化された回答を返す
  ```

  ```python Data Enrichment theme={null}
  # 会社データを強化
  result = asyncio.run(answer_question.ainvoke({
      "task": "Find the CEO and headquarters of Stripe",
      "json_schema": {
          "ceo_name": "",
          "headquarters": "",
          "founded_year": ""
      }
  }))

  # 不確実性を"NOT_FOUND"値で処理
  ```
</CodeGroup>

### extract\_urls

サイト構造分析のためにウェブサイトからすべてのURLを抽出。

<ParamField path="url" type="string" required>
  URLを抽出するウェブサイトのURL
</ParamField>

<ParamField path="search_query" type="string">
  URLをフィルタリングするためのオプションの検索クエリ
</ParamField>

<ParamField path="top_n" type="integer">
  返されるURLの数を制限
</ParamField>

<ParamField path="include_urls" type="array">
  含めるグロブパターン（例：\["/blog/\*\*"]）
</ParamField>

<ParamField path="exclude_urls" type="array">
  除外するグロブパターン（例：\["/admin/\*\*"]）
</ParamField>

<CodeGroup>
  ```python Extract All URLs theme={null}
  from langchain_olostep import extract_urls
  import asyncio

  # ウェブサイトからすべてのURLを取得
  result = asyncio.run(extract_urls.ainvoke({
      "url": "https://example.com",
      "top_n": 100
  }))

  print(result)
  # 戻り値: {"urls": [...], "total_urls": 100, ...}
  ```

  ```python Filter URLs theme={null}
  # ブログURLのみを取得
  result = asyncio.run(extract_urls.ainvoke({
      "url": "https://example.com",
      "include_urls": ["/blog/**"],
      "exclude_urls": ["/admin/**", "/private/**"],
      "top_n": 50
  }))
  ```
</CodeGroup>

### crawl\_website

リンクをたどってウェブサイト全体を自動的に発見しスクレイプ。

<ParamField path="start_url" type="string" required>
  クロールの開始URL
</ParamField>

<ParamField path="max_pages" type="integer" default="100">
  クロールする最大ページ数
</ParamField>

<ParamField path="include_urls" type="array">
  含めるグロブパターン（例：\["/\*\*"]で全て）
</ParamField>

<ParamField path="exclude_urls" type="array">
  除外するグロブパターン（例：\["/admin/\*\*"]）
</ParamField>

<ParamField path="max_depth" type="integer">
  start\_urlからクロールする最大深度
</ParamField>

<ParamField path="include_external" type="boolean" default="false">
  外部URLを含める
</ParamField>

<CodeGroup>
  ```python Crawl Website theme={null}
  from langchain_olostep import crawl_website
  import asyncio

  # ドキュメントサイト全体をクロール
  result = asyncio.run(crawl_website.ainvoke({
      "start_url": "https://docs.example.com",
      "max_pages": 100
  }))

  print(result)
  # 戻り値: {"crawl_id": "crawl_xxx", "status": "in_progress", ...}
  ```

  ```python With Filters theme={null}
  # URLフィルタを使用してクロール
  result = asyncio.run(crawl_website.ainvoke({
      "start_url": "https://example.com",
      "max_pages": 200,
      "include_urls": ["/**"],
      "exclude_urls": ["/admin/**", "/private/**"],
      "max_depth": 3
  }))
  ```
</CodeGroup>

## LangChainエージェント統合

ウェブを検索しスクレイプできるインテリジェントなエージェントを構築：

<CodeGroup>
  ```python Basic Agent theme={null}
  from langchain.agents import initialize_agent, AgentType
  from langchain_openai import ChatOpenAI
  from langchain_olostep import (
      scrape_website,
      answer_question,
      extract_urls
  )

  # Olostepツールでエージェントを作成
  tools = [scrape_website, answer_question, extract_urls]
  llm = ChatOpenAI(model="gpt-4o-mini")

  agent = initialize_agent(
      tools=tools,
      llm=llm,
      agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
      verbose=True
  )

  # エージェントを使用
  result = agent.run("""
  Research the company at https://company.com:
  1. Scrape their about page
  2. Search for their latest funding round
  3. Extract all their product pages
  """)

  print(result)
  ```
</CodeGroup>

## LangGraph統合

LangGraphで複雑なマルチステップワークフローを構築：

<CodeGroup>
  ```python Research Agent theme={null}
  from langgraph.graph import StateGraph, END
  from langchain_olostep import (
      scrape_website,
      scrape_batch,
      answer_question,
      extract_urls
  )
  from langchain_openai import ChatOpenAI
  import json

  def create_research_agent():
      workflow = StateGraph(dict)
      
      def discover_pages(state):
          # ターゲットサイトからすべてのURLを抽出
          result = extract_urls.invoke({
              "url": state["target_url"],
              "include_urls": ["/product/**"],
              "top_n": 50
          })
          state["urls"] = json.loads(result)["urls"]
          return state
      
      def scrape_pages(state):
          # 発見されたページをバッチでスクレイプ
          result = scrape_batch.invoke({
              "urls": state["urls"],
              "format": "markdown"
          })
          state["batch_id"] = json.loads(result)["batch_id"]
          return state
      
      def answer_questions(state):
          # データに関する質問にAIを使用して回答
          result = answer_question.invoke({
              "task": state["research_question"],
              "json_schema": state["desired_format"]
          })
          state["answer"] = json.loads(result)["answer"]
          return state
      
      workflow.add_node("discover", discover_pages)
      workflow.add_node("scrape", scrape_pages)
      workflow.add_node("analyze", answer_questions)
      
      workflow.set_entry_point("discover")
      workflow.add_edge("discover", "scrape")
      workflow.add_edge("scrape", "analyze")
      workflow.add_edge("analyze", END)
      
      return workflow.compile()

  # エージェントを使用
  agent = create_research_agent()
  result = agent.invoke({
      "target_url": "https://store.com",
      "research_question": "What are the top 5 most expensive products?",
      "desired_format": {
          "products": [{"name": "", "price": "", "url": ""}]
      }
  })
  ```
</CodeGroup>

## 高度なユースケース

### データ強化

スプレッドシートデータをウェブ情報で強化：

```python theme={null}
from langchain_olostep import answer_question

companies = ["Stripe", "Shopify", "Square"]

for company in companies:
    result = answer_question.invoke({
        "task": f"Find information about {company}",
        "json_schema": {
            "ceo": "",
            "headquarters": "",
            "employee_count": "",
            "latest_funding": ""
        }
    })
    print(f"{company}: {result}")
```

### Eコマース製品スクレイピング

特殊なパーサーで製品データをスクレイプ：

```python theme={null}
from langchain_olostep import scrape_website

# Amazon製品をスクレイプ
result = scrape_website.invoke({
    "url": "https://www.amazon.com/dp/PRODUCT_ID",
    "parser": "@olostep/amazon-product",
    "format": "json"
})
# 構造化された製品データを返す：価格、タイトル、評価など
```

### SEO監査

SEOのためにウェブサイト全体を分析：

```python theme={null}
from langchain_olostep import extract_urls, scrape_batch
import json

# 1. すべてのページを発見
urls_result = extract_urls.invoke({
    "url": "https://yoursite.com",
    "top_n": 1000
})

# 2. すべてのページをスクレイプ
urls = json.loads(urls_result)["urls"]
batch_result = scrape_batch.invoke({
    "urls": urls,
    "format": "html"
})
```

### ドキュメントスクレイピング

ドキュメントをクロールして抽出：

```python theme={null}
from langchain_olostep import crawl_website

# ドキュメントサイト全体をクロール
result = crawl_website.invoke({
    "start_url": "https://docs.example.com",
    "max_pages": 500,
    "include_urls": ["/docs/**"],
    "exclude_urls": ["/api/**", "/v1/**"]
})
```

## 専門パーサー

Olostepは人気のあるウェブサイト向けの事前構築されたパーサーを提供：

* `@olostep/google-search` - Google検索結果

`parser`パラメータで使用：

```python theme={null}
scrape_website.invoke({
    "url": "https://www.google.com/search?q=alexander+the+great&gl=us&hl=en",
    "parser": "@olostep/google-search"
})
```

## エラーハンドリング

```python theme={null}
from langchain_core.exceptions import LangChainException

try:
    result = await scrape_website.ainvoke({
        "url": "https://example.com"
    })
except LangChainException as e:
    print(f"スクレイピング失敗: {e}")
```

## ベストプラクティス

<AccordionGroup>
  <Accordion title="複数のURLに対してバッチ処理を使用">
    3-5以上のURLをスクレイプする場合は、複数の`scrape_website`呼び出しの代わりに`scrape_batch`を使用してください。バッチ処理ははるかに高速でコスト効率が高いです。
  </Accordion>

  <Accordion title="適切なタイムアウトを設定">
    JavaScriptが多用されているサイトの場合、`wait_before_scraping`パラメータを使用してください（2000-5000msが一般的）。これにより、動的コンテンツが完全に読み込まれます。
  </Accordion>

  <Accordion title="専門パーサーを使用">
    人気のあるウェブサイト（Amazon、LinkedIn、Google）については、事前構築されたパーサーを使用して自動的に構造化データを取得してください。
  </Accordion>

  <Accordion title="効率的にURLをフィルタリング">
    `extract_urls`または`crawl_website`を使用する際は、グロブパターンを使用して関連するページに焦点を当て、不要な処理を避けてください。
  </Accordion>

  <Accordion title="レート制限を処理">
    レート制限エラーに対して指数バックオフを実装してください。APIは内部でほとんどのレート制限を自動的に処理します。
  </Accordion>
</AccordionGroup>

## サポート

* **PyPIパッケージ**: [langchain-olostep](https://pypi.org/project/langchain-olostep/)
* **ドキュメント**: [docs.olostep.com](https://docs.olostep.com)
* **問題**: [GitHub Issues](https://github.com/olostep/langchain-olostep/issues)
* **メール**: [info@olostep.com](mailto:info@olostep.com)

## 関連リソース

<CardGroup cols={2}>
  <Card title="Scrapes API" icon="file-lines" href="/features/scrapes">
    Scrapesエンドポイントについて学ぶ
  </Card>

  <Card title="Batches API" icon="layer-group" href="/features/batches">
    Batchesエンドポイントについて学ぶ
  </Card>

  <Card title="Answers API" icon="question" href="/features/answers">
    Answersエンドポイントについて学ぶ
  </Card>

  <Card title="Maps API" icon="map" href="/features/maps">
    Mapsエンドポイントについて学ぶ
  </Card>

  <Card title="Crawls API" icon="spider-web" href="/features/crawls">
    Crawlsエンドポイントについて学ぶ
  </Card>

  <Card title="Python SDK" icon="python" href="/sdks/python">
    Python SDKを探索
  </Card>

  <Card title="LangChain Website" icon="link" href="https://www.langchain.com">
    LangChainプラットフォーム
  </Card>
</CardGroup>
