OlostepのLangChainインテグレーションは、あらゆるウェブサイトからデータを検索、スクレイピング、分析、構造化するAIエージェントを構築するための包括的なツールを提供します。LangChainおよびLangGraphアプリケーションに最適です。
このインテグレーションは、Olostep APIのすべての5つの機能にアクセスできます:
スクレイプ 単一のURLからコンテンツを複数の形式(Markdown、HTML、JSON、テキスト)で抽出
バッチ 最大10,000のURLを並行して処理。バッチジョブは5〜8分で完了
アンサー 自然言語クエリと構造化された出力によるAI駆動のウェブ検索
マップ サイト構造分析のためにウェブサイトからすべてのURLを抽出
クロール リンクをたどってウェブサイト全体を自動的に発見しスクレイピング
インストール
pip install langchain-olostep
セットアップ
Olostep APIキーを環境変数として設定します:
export OLOSTEP_API_KEY = "your_olostep_api_key_here"
APIキーはOlostep Dashboard から取得してください。
利用可能なツール
scrape_website
単一のURLからコンテンツを抽出します。複数の形式とJavaScriptレンダリングをサポートします。
出力形式:markdown、html、json、またはtext
地域特有のコンテンツ用の国コード(例:“US”、“GB”、“CA”)
JavaScriptレンダリングの待機時間(0-10000ミリ秒)
特殊な抽出用のオプションのパーサーID(例:“@olostep/amazon-product”)
Basic Scraping
With JavaScript
With Parser
from langchain_olostep import scrape_website
import asyncio
# ウェブサイトをスクレイプ
content = asyncio.run(scrape_website.ainvoke({
"url" : "https://example.com" ,
"format" : "markdown"
}))
print (content)
scrape_batch
複数のURLを並行して処理します(一度に最大10,000)。
すべてのURLの出力形式:markdown、html、json、またはtext
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)
# Returns: {"batch_id": "batch_xxx", "status": "in_progress", ...}
answer_question
ウェブを検索し、ソース付きのAI駆動の回答を得ます。データの充実化や研究に最適です。
望ましい出力形式を記述するオプションのJSONスキーマ辞書/文字列
Simple Question
Structured Output
Data Enrichment
from langchain_olostep import answer_question
import asyncio
# 簡単な質問をする
result = asyncio.run(answer_question.ainvoke({
"task" : "What is the capital of France?"
}))
print (result)
# Returns: {"answer": {"result": "Paris"}, "sources": [...]}
サイト構造分析のためにウェブサイトからすべてのURLを抽出します。
URLをフィルタリングするためのオプションの検索クエリ
含めるグロブパターン(例:[“/blog/**”])
除外するグロブパターン(例:[“/admin/**”])
Extract All URLs
Filter URLs
from langchain_olostep import extract_urls
import asyncio
# ウェブサイトからすべてのURLを取得
result = asyncio.run(extract_urls.ainvoke({
"url" : "https://example.com" ,
"top_n" : 100
}))
print (result)
# Returns: {"urls": [...], "total_urls": 100, ...}
crawl_website
リンクをたどってウェブサイト全体を自動的に発見しスクレイピングします。
除外するグロブパターン(例:[“/admin/**”])
Crawl Website
With Filters
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)
# Returns: {"crawl_id": "crawl_xxx", "status": "in_progress", ...}
LangChainエージェントインテグレーション
ウェブを検索しスクレイピングできるインテリジェントなエージェントを構築します:
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)
LangGraphインテグレーション
LangGraphを使用して複雑なマルチステップワークフローを構築します:
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" : "" }]
}
})
高度なユースケース
データエンリッチメント
スプレッドシートデータをウェブ情報で充実化:
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コマース製品スクレイピング
特殊なパーサーを使用して製品データをスクレイプ:
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のためにウェブサイト全体を分析:
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"
})
ドキュメントスクレイピング
ドキュメントをクロールして抽出:
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パラメータで使用します:
scrape_website.invoke({
"url" : "https://www.google.com/search?q=alexander+the+great&gl=us&hl=en" ,
"parser" : "@olostep/google-search"
})
エラーハンドリング
from langchain_core.exceptions import LangChainException
try :
result = await scrape_website.ainvoke({
"url" : "https://example.com"
})
except LangChainException as e:
print ( f "Scraping failed: { e } " )
ベストプラクティス
3〜5以上のURLをスクレイプする場合は、複数のscrape_website呼び出しの代わりにscrape_batchを使用してください。バッチ処理ははるかに高速でコスト効率が高いです。
JavaScriptを多用するサイトには、wait_before_scrapingパラメータを使用してください(2000〜5000msが一般的です)。これにより動的コンテンツが完全に読み込まれます。
人気のあるウェブサイト(Amazon、LinkedIn、Google)には、事前構築されたパーサーを使用して自動的に構造化データを取得してください。
extract_urlsやcrawl_websiteを使用する際は、関連するページに焦点を当て、不要な処理を避けるためにグロブパターンを使用してください。
レート制限エラーに対して指数バックオフを実装します。APIは内部でほとんどのレート制限を自動的に処理します。
サポート
関連リソース