> ## Documentation Index
> Fetch the complete documentation index at: https://docs.olostep.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 一次获取多个网站的内容

> 开始批量抓取，在5-7分钟内从最多10万个URL中提取内容

## 概述

Olostep的[Batches](https://docs.olostep.com/api-reference/batches/create)端点允许你启动最多10,000个URL的批量抓取，并在5-7分钟内获取内容。你可以同时启动最多10个批次，一次性从100,000个URL中提取内容。如果你需要更大的规模，请联系我们。

如果你已经有想要处理的URL，例如，为分析聚合数据、构建专业搜索工具或监控多个网站的变化，这将非常有用。

在本指南中，我们将演示如何使用URL列表启动批量抓取并以markdown格式检索内容。

## 完整代码的要点

这是一个包含所有代码的要点，你可以复制并粘贴以尝试使用Olostep进行批量抓取：
[https://gist.github.com/olostep/e903f2e4fc28f8093b834b4df68b8031](https://gist.github.com/olostep/e903f2e4fc28f8093b834b4df68b8031)

在这个要点中，我们展示了如何启动包含5个谷歌搜索查询的批次，检查状态，并为每个项目检索内容。

## 前提条件

在开始之前，请确保你具备以下条件：

* 有效的Olostep API密钥。你可以通过在[Olostep](https://www.olostep.com/dashboard)注册来获取。
* 系统上已安装Python。
* `requests`和`hashlib`库（如果需要，使用`pip install requests`安装`requests`）。

## 步骤1：从本地URL创建批次

如果你已经有想要处理的URL列表，可以直接在脚本中定义它们。否则，你可以从文件或数据库中读取它们。

```python filename="start_batch.py" theme={null}
import requests
import hashlib

API_KEY = "YOUR_API_KEY"

def create_hash_id(url):
    return hashlib.sha256(url.encode()).hexdigest()[:16]

def compose_items_array():
    urls = [
        "https://www.google.com/search?q=nikola+tesla&gl=us&hl=en",
        "https://www.google.com/search?q=alexander+the+great&gl=us&hl=en",
        "https://www.google.com/search?q=google+solar+eclipse&gl=us&hl=en",
        "https://www.google.com/search?q=crispr&gl=us&hl=en",
        "https://www.google.com/search?q=genghis%20khan&gl=us&hl=en"
    ]
    return [{"custom_id": create_hash_id(url), "url": url} for url in urls]

def start_batch(items):
    payload = {
        "items": items
    }
    headers = {"Authorization": f"Bearer {API_KEY}"}
    response = requests.post(
        "https://api.olostep.com/v1/batches",
        headers=headers,
        json=payload
    )
    return response.json()["id"]

if __name__ == "__main__":
    items = compose_items_array()
    batch_id = start_batch(items)
    print("Batch started. ID:", batch_id)
```

## 步骤2：监控批次状态

批次启动后，你可以使用启动批次时返回的`batch_id`监控其状态。

```python filename="check_status.py" theme={null}
import requests

def check_batch_status(batch_id):
    headers = {"Authorization": f"Bearer {API_KEY}"}
    response = requests.get(
        f"https://api.olostep.com/v1/batches/{batch_id}",
        headers=headers
    )
    return response.json()["status"]
```

你可以每隔几秒（例如10秒）轮询状态，直到批次完成：

```python theme={null}
import time

def recursive_check(batch_id):
    status = check_batch_status(batch_id)
    print("Status:", status)
    if status == "completed":
        print("Batch is complete!")
    else:
        time.sleep(60)
        recursive_check(batch_id)
```

## 步骤3：检索已完成的项目

一旦批次标记为完成，获取已处理的项目。

```python theme={null}
import requests

def get_completed_items(batch_id):
    headers = {"Authorization": f"Bearer {API_KEY}"}
    response = requests.get(
        f"https://api.olostep.com/v1/batches/{batch_id}/items",
        headers=headers
    )
    return response.json()["items"]
```

每个项目将包含一个`retrieve_id`，你可以使用它来获取抓取的内容。

```python theme={null}
items = get_completed_items(batch_id)
for item in items:
    print(f"URL: {item['url']}\nCustom ID: {item['custom_id']}\nRetrieve ID: {item['retrieve_id']}\n---")
```

## 步骤4：检索内容

使用`retrieve_id`获取以markdown、html或json格式提取的内容。以下是以markdown格式检索内容的示例：

```python filename="retrieve_content.py" theme={null}
def retrieve_content(retrieve_id):
    url = "https://api.olostep.com/v1/retrieve"
    headers = {"Authorization": f"Bearer {API_KEY}"}
    params = {"retrieve_id": retrieve_id}

    response = requests.get(url, headers=headers, params=params)
    return response.json()

# Example usage:
items = get_completed_items(batch_id)
for item in items:
    content = retrieve_content(item['retrieve_id'])
    print(content)
```

## 托管内容

我们还会托管内容7天，因此你可以多次检索而无需重新抓取。
[markdown内容](https://olostep-storage.s3.us-east-1.amazonaws.com/markDown_p64bro0q3n_924d0fa125efcc5c.txt)的托管URL示例

## 示例用例

### 1. 构建搜索引擎

使用Olostep从特定行业网站（法律、医疗、AI）中提取内容并构建可搜索的数据库。

### 2. 网站监控

通过安排每日批量抓取来监控产品可用性、价格变化或新闻更新。

### 3. 社交媒体监控

抓取论坛或内容来源中对你的品牌或关键词的提及，并提取结构化数据。

### 4. 聚合器

通过从多个来源提取数据，构建招聘板、新闻聚合器或房地产列表平台。

## 结论

通过批量抓取，你可以快速高效地从最多10万个URL中提取内容。无论你是在构建搜索工具、聚合器还是监控系统，Olostep Batches都能简化工作。

想要只提取结构化数据？使用[Parsers](https://docs.olostep.com/features/structured-content/parsers)获取你需要的字段。需要帮助？请联系`info@olostep.com`获取支持或让我们为你的用例编写自定义脚本。
