# AI-Scraper

## 概览

[**AI-Scraper**](https://aistudio.oxylabs.io/apps/scrape) 是一种抓取工具，可从单个网页中提取数据。它根据自然语言提示识别并解析相关信息，然后以以下任一形式交付结果 **JSON** （用于自动化和 API）或 **Markdown** 格式（最适合可读输出和 AI 工作流）。

这个 AI 抓取器无需 CSS/XPath 选择器或自定义解析器，因此可以与各种自动化流水线无缝集成。 **自动生成 schema** 以及灵活的输出格式，为用户提供了一种轻松提取干净、结构化数据的方法，而无需维护解析逻辑。

你可以预览该工具 [**这里**](https://aistudio.oxylabs.io/apps/scrape) 并通过我们的 Python/JavaScript SDK、MCP 服务器或第三方集成将其整合到你的工作流中。

## 主要功能

* **基于自然语言提示的提取** – 用通俗英语定义你的需求，抓取代理就会检索相关信息。
* **多种输出格式** – 选择 JSON 用于结构化工作流，或选择 Markdown 用于人类可读结果和 AI 工作流。
* **自动生成 schema** – 根据提示自动生成 schema，或手动定义以实现精确的 JSON 解析。
* **适用于任何公开网页** – 可从电商、新闻、博客或任何其他可访问来源中提取。

## 工作原理

要使用 AI-Scraper 抓取网页，请按以下步骤操作：

1. **提供网页 URL** 你想抓取的。
2. **描述要提取的数据** 使用自然语言（例如：“获取所有产品名称和价格”）。
3. **选择输出格式** – 结构化 JSON 或 Markdown。
4. **（可选）定义 schema** – 让 AI-Scraper 自动生成，或提供你自己的 OpenAPI schema，以获得你想要的精确结构。

### 安装

开始之前，请确保你可以访问 AI Studio API 密钥（或 [获得免费试用](https://aistudio.oxylabs.io/register) （含 1000 credits））并且已安装 `Python v3.10` 或更高版本。你可以使用 pip 安装 `oxylabs-ai-studio` 包：

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

### 代码示例（Python）

以下示例展示了如何使用 `AiScraper` 从示例页面提取数据。

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

# 使用你的 API 密钥初始化 AI Scraper
scraper = AiScraper(api_key="YOUR_API_KEY")

# 从自然语言自动生成模式
schema = scraper.generate_schema(prompt="想解析 developer、platform、type、price、game title 和 genre (array)")
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,
    geo_location="US",
)
# 将抓取输出以 JSON 形式打印
print("Results:")
print(json.dumps(result.data, indent=2))
```

在我们的文档中了解更多关于 AI-Scraper 和 Oxylabs AI Studio Python SDK 的信息 [PyPI repository](https://pypi.org/project/oxylabs-ai-studio/)。你也可以查看我们的 [AI Studio JavaScript SDK](https://github.com/oxylabs/oxylabs-ai-studio-js) 指南，适用于 JS 用户。

### 请求参数

| 参数                                                 | 描述                               | 默认值        |
| -------------------------------------------------- | -------------------------------- | ---------- |
| <mark style="background-color:green;">`url`</mark> | 要抓取的目标 URL                       | –          |
| `output_format`                                    | 输出格式（`json`, `markdown`)         | `markdown` |
| `schema`                                           | 用于结构化提取的 OpenAPI schema（JSON 必填） | –          |
| `render_javascript`                                | 启用 JavaScript 渲染                 | `False`    |
| `geo_location`                                     | ISO2 格式的代理位置                     | –          |

&#x20;    – 必填参数

### 输出示例

AI-Scraper 可以返回已解析、可直接使用的输出，便于轻松集成到你的应用中。

以下是其 JSON 输出的样子：

```json
{
  "games": [
    {
      "developer": "Nintendo EAD Tokyo",
      "platform": "wii",
      "type": "singleplayer",
      "price": 91.99,
      "title": "Super Mario Galaxy 2",
      "genre": [
        "Action",
        "Platformer"
      ]
    },
    {
      "developer": "Eidos Interactive",
      "platform": "wii",
      "type": null,
      "price": 80.99,
      "title": "Death Jr.: Root of Evil",
      "genre": [
        "Action",
        "Platformer",
        "3D"
      ]
    }
}
```

或者，你也可以设置 `output_format`  到 `markdown` 以接收 Markdown 格式的结果，而不是 JSON。

## 实际使用场景

AI-Scraper 可应用于多种数据收集任务：

1. **提取产品详情** – 从电商网站收集产品名称、描述和价格。
2. **解析新闻文章** – 获取文章标题、日期、作者和正文内容。
3. **抓取价格页面** – 为竞争对手分析或市场调研收集结构化定价信息。
4. **提取职位发布** – 从招聘门户网站获取职位名称、地点、薪资和发布日期。

<br>


---

# Agent Instructions: 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:

```
GET https://developers.oxylabs.io/products/cn/ai-studio/ai-scraper.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
