> 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

了解如何使用 AI Studio 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 密钥

## 安装

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

## 用法

### Crawl (`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="Find all pages with proxy products pricing",
    output_format="markdown",
    render_javascript=False,
    return_sources_limit=3,
    geo_location="US",
)
print("Results:")
for item in result.data:
    print(item, "\n")
```

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

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

### Scrape (`AiScraper.scrape`)

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

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

schema = scraper.generate_schema(prompt="want to parse developer, platform, type, price game title, genre (array) and 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` (字典 | 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="game name, platform, review stars and price"
)
print("schema: ", schema)

prompt = "Find if there is game 'super mario odyssey' in the store. If there is, find the price. Use search bar to find the game."
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` (字典 | 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 = "lasagna recipe"
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": "job ad pages",
    "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 格式的代理位置。

### 使用示例

请参阅 [示例](https://github.com/oxylabs/oxylabs-ai-studio-py/tree/main/examples) 我们 GitHub 中的文件夹，包含每个方法的详细使用示例（每个方法都有对应的 `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.
