> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/integrations/cn/ai-studio-integrations/python-sdk.md).

# Python SDK

我们提供一个简单的 Python SDK，可无缝与 [Oxylabs AI Studio API](https://aistudio.oxylabs.io/) 服务交互，包括 AI-Scraper、AI-Crawler、AI-Browser-Agent 以及其他数据提取工具。

## 要求

* `python 3.10` 及以上
* AI Studio API key

## 安装

```
pip install oxylabs-ai-studio
```

## 用法

### 爬取（`AiCrawler.crawl`)

```python
from oxylabs_ai_studio.apps.ai_crawler import AiCrawler

crawler = AiCrawler(api_key="<API_KEY>")

url = "https://oxylabs.io"
result = crawler.crawl(
    url=url,
    user_prompt="查找所有包含代理产品价格的页面",
    output_format="markdown",
    render_javascript=False,
    return_sources_limit=3,
    geo_location="US",
)
print("结果:")
for item in result.data:
    print(item, "\n")
```

#### **输入参数**

* `url` （字符串）：要爬取的起始 URL（**必需**)
* `user_prompt` （字符串）：用于指导提取的自然语言提示（**必需**)
* `output_format` （Literal\["json", "markdown"]）：输出格式（默认："markdown"）
* `schema` （dict | None）：用于结构化提取的 OpenAPI schema（当 output\_format 为 "json" 时必需）
* `render_javascript` （布尔值）：渲染 JavaScript（默认：False）
* `return_sources_limit` （整数）：返回的最大来源数量（默认：25）
* `geo_location` （字符串）：ISO2 格式的代理位置。

### 抓取（`AiScraper.scrape`)

```python
from oxylabs_ai_studio.apps.ai_scraper import AiScraper

scraper = AiScraper(api_key="<API_KEY>")

schema = scraper.generate_schema(prompt="想解析 developer、platform、type、price、game title、genre（数组）和 description")
print(f"Generated schema: {schema}")

url = "https://sandbox.oxylabs.io/products/3"
result = scraper.scrape(
    url=url,
    output_format="json",
    schema=schema,
    render_javascript=False,
)
print(result)
```

#### **输入参数**

* `url` （字符串）：要抓取的目标 URL（**必需**)
* `output_format` （Literal\["json", "markdown"]）：输出格式（默认："markdown"）
* `schema` （dict | None）：用于结构化提取的 OpenAPI schema（当 output\_format 为 "json" 时必需）
* `render_javascript` （布尔值）：渲染 JavaScript（默认：False）
* `geo_location` （字符串）：ISO2 格式的代理位置。

### Browser Agent（`BrowserAgent.run`)

```python
from oxylabs_ai_studio.apps.browser_agent import BrowserAgent

browser_agent = BrowserAgent(api_key="<API_KEY>")

schema = browser_agent.generate_schema(
    prompt="游戏名称、平台、评分星级和价格"
)
print("schema: ", schema)

prompt = "查找商店中是否有游戏 'super mario odyssey'。如果有，找出价格。使用搜索栏查找该游戏。"
url = "https://sandbox.oxylabs.io/"
result = browser_agent.run(
    url=url,
    user_prompt=prompt,
    output_format="json",
    schema=schema,
)
print(result.data)
```

#### **输入参数**

* `url` （字符串）：要浏览的起始 URL（**必需**)
* `user_prompt` （字符串）：用于提取的自然语言提示（**必需**)
* `output_format` （Literal\["json", "markdown", "html", "screenshot"]）：输出格式（默认："markdown"）
* `schema` （dict | None）：用于结构化提取的 OpenAPI schema（当 output\_format 为 "json" 时必需）
* `geo_location` （字符串）：ISO2 格式的代理位置。

### 搜索（`AiSearch.search`)

```python
from oxylabs_ai_studio.apps.ai_search import AiSearch


search = AiSearch(api_key="<API_KEY>")

query = "千层面食谱"
result = search.search(
    query=query,
    limit=5,
    render_javascript=False,
    return_content=True,
)
print(result.data)
```

#### **输入参数**

* `query` （字符串）：要搜索的内容（**必需**)
* `limit` （整数）：返回结果的最大数量（默认：10，最大：50）
* `render_javascript` （布尔值）：渲染 JavaScript（默认：False）
* `return_content` （布尔值）：是否在结果中返回 Markdown 内容（默认：True）
* `geo_location` （字符串）：ISO2 格式的搜索代理位置。

#### 地图（`AiMap.map`)

```python
from oxylabs_ai_studio.apps.ai_map import AiMap


ai_map = AiMap(api_key="<API_KEY>")
payload = {
    "url": "https://career.oxylabs.io",
    "user_prompt": "招聘广告页面",
    "return_sources_limit": 10,
    "geo_location": None,
    "render_javascript": False,
}
result = ai_map.map(**payload)
print(result.data)
```

#### **输入参数**

* `url` （字符串）：要爬取的起始 URL（**必需**)
* `user_prompt` （字符串）：用于指导提取的自然语言提示（**必需**)
* `render_javascript` （布尔值）：渲染 JavaScript（默认：False）
* `return_sources_limit` （整数）：返回的最大来源数量（默认：25）
* `geo_location` （字符串）：ISO2 格式的代理位置。

### 用法示例

请参阅我们的 GitHub 中的 [examples](https://github.com/oxylabs/oxylabs-ai-studio-py/tree/main/examples) 文件夹，了解每个方法的详细用法示例（每个方法都有对应的 `async` 版本）。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers.oxylabs.io/integrations/cn/ai-studio-integrations/python-sdk.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
