OpenAI Agents SDK web scraping
OpenAI Agents SDK agent को Spicrawl MCP server से connect करें। फिर agent web pages को Markdown में पढ़ सकता है।
Set up करें
openai-agentsinstall करें।SPICRAWL_API_KEYऔरOPENAI_API_KEYset करें।URL और
paramsमेंAuthorizationheader के साथMCPServerStreamableHttpखोलें।Model को कौन-से tools दिखें, यह सीमित करने के लिए
create_static_tool_filterइस्तेमाल करें, और server को अपनेAgentमें दें।
उदाहरण: एक pricing page पढ़ें
import asyncio
import os
from agents import Agent, Runner
from agents.mcp import MCPServerStreamableHttp, create_static_tool_filter
async def main() -> None:
async with MCPServerStreamableHttp(
name="spicrawl",
params={
"url": "https://mcp.spicrawl.com/mcp",
"headers": {"Authorization": f"Bearer {os.environ['SPICRAWL_API_KEY']}"},
},
cache_tools_list=True,
tool_filter=create_static_tool_filter(
allowed_tool_names=["spicrawl_scrape", "spicrawl_batch_submit"]
),
) as server:
agent = Agent(
name="Page reader",
instructions="Use the Spicrawl tools to read web pages.",
mcp_servers=[server],
)
result = await Runner.run(
agent,
"Read https://example.com/pricing as markdown and list the plans.",
)
print(result.final_output)
asyncio.run(main())Server में OAuth नहीं है, इसलिए key हमेशा headers में भेजें। अगर 401 मिले, तो जाँचें कि agent चलाने वाली process में SPICRAWL_API_KEY set है।
Tool list छोटी रखें
सभी 25 tool schemas हर model call पर भेजे जाते हैं। spicrawl_scrape रखें, और कई URLs के लिए spicrawl_batch_submit, spicrawl_batch_status और spicrawl_batch_results जोड़ें।
अगर आपका code पहले से जानता है कि कौन-सा URL fetch करना है, तो POST /v1/scrape को call करने वाला एक सादा function ज़्यादा आसान है। इसमें tool-schema tokens नहीं लगते और इसे test करना आसान है।
यह क्या कर सकता है
Server agent को 25 spicrawl_* tools देता है। हर call आपकी API key के तहत चलती है, उन्हीं limits और credits के साथ जो सीधी API call में होते हैं।
- एक page पढ़ें:
spicrawl_scrapeMarkdown (default), text, HTML, JSON या PDF लौटाता है। - कई pages पढ़ें:
spicrawl_batch_submitएक बार में 10,000 URLs तक queue करता है। इसके बादspicrawl_batch_statusऔरspicrawl_batch_resultsचलाएँ। - Login के पीछे के pages:
spicrawl_session_createcalls के बीच cookies और storage बनाए रखता है। - Failure debug करें:
spicrawl_requests_listऔरspicrawl_request_get। - Usage जाँचें:
spicrawl_usage_summaryऔर बाकी usage tools। इनके लिएreadscope वाली key चाहिए। - चीज़ें खोजें:
spicrawl_docs_searchऔरspicrawl_docs_readSpicrawl docs खोजते और पढ़ते हैं।
एक tool, spicrawl_browser_connect_url, listed है लेकिन जल्द आ रहा है। spicrawl_scrape पर rendering argument render है, न कि API का js_render।
जानने लायक बातें
- Crawling नहीं। Spicrawl links follow नहीं करता और pages नहीं ढूँढता। Agent को URLs दें।
- प्रति request credits। Plain fetch 1 credit का है। JavaScript rendering 3 का। Full-browser rendering 8 का।
- Failures free हैं। Fail हुई request की कीमत 0 credits है।
- Cache समय बचाता है, credits नहीं। Cache हिट की कीमत उसी fetch जितनी है जिसने उसे store किया था।
- हर call की सीमा तय करें।
max_costset करें, और जो request ज़्यादा खर्च कर सकती है उसे चलने से पहले ही refuse कर दिया जाता है। - जल्द आ रहा है: managed proxies, stealth mode, एक remote browser और एक plain-language prompt से AI extraction। आज आप अपना proxy ला सकते हैं।
अक्सर पूछे जाने वाले सवाल
मुझे कौन सा package चाहिए?
openai-agents। Example चलाने से पहले SPICRAWL_API_KEY और OPENAI_API_KEY सेट करें।
मैं कैसे सीमित करूँ कि model कौन से tools देखे?
Pass tool_filter=create_static_tool_filter(allowed_tool_names=[...]). The example keeps spicrawl_scrape and spicrawl_batch_submit.
Agent के connect होने पर मुझे 401 क्यों मिलता है?
Header गायब है या key गलत है। Header को code में SPICRAWL_API_KEY से बनाएँ और जाँचें कि variable उस process में सेट है जो agent चलाता है।
Tool call fail होने पर क्या होता है?
Error model तक API के code के साथ MCP tool error के रूप में पहुँचती है। Failed calls पर 0 credits लगते हैं।
हर page की कीमत कितनी है?
Plain fetch की कीमत 1 credit है, JavaScript rendering की 3 और full-browser rendering की 8। Failed requests की कीमत 0। Cache hit की कीमत उतनी ही है जितनी उस fetch की जिसने इसे store किया। Agent से हर request पर max_cost सेट करने को कहें ताकि वह आपकी उम्मीद से ज़्यादा खर्च न करे।