跳转到主要内容

概述

Olostep的Batches端点允许您启动最多10,000个URL的批量抓取,并在5-7分钟内获取内容。您可以同时启动最多10个批次,一次性从100,000个URL中提取内容。如果需要更大的规模,请联系我们。 如果您已经有想要处理的URL,例如用于聚合数据进行分析、构建专业搜索工具或监控多个网站的变化,这将非常有用。 在本指南中,我们将逐步介绍如何使用URL列表启动批量抓取并以markdown格式检索内容。

完整代码的要点

这是一个包含所有代码的要点,您可以复制并粘贴以尝试使用Olostep进行批量抓取: https://gist.github.com/olostep/e903f2e4fc28f8093b834b4df68b8031 在这个要点中,我们展示了如何使用5个谷歌搜索查询启动批量抓取、检查状态以及检索每个项目的内容。

前提条件

在开始之前,请确保您具备以下条件:
  • 有效的Olostep API密钥。您可以通过在Olostep注册来获取。
  • 系统上已安装Python。
  • requestshashlib库(如果需要,使用pip install requests安装requests)。

步骤1:从本地URL创建批次

如果您已经有想要处理的URL列表,可以直接在脚本中定义它们。否则,您可以从文件或数据库中读取它们。
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监控其状态。
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秒)轮询状态,直到批次完成:
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:检索已完成的项目

一旦批次标记为完成,获取处理过的项目。
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,您可以使用它来获取抓取的内容。
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格式检索内容的示例:
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内容的托管URL示例

示例用例

1. 构建搜索引擎

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

2. 网站监控

通过安排每日批量抓取,监控多个网站的产品可用性、价格变化或新闻更新。

3. 社交媒体监控

抓取论坛或内容源中提到的品牌或关键词,并提取结构化数据。

4. 聚合器

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

结论

通过批量抓取,您可以快速高效地从最多10万个URL中提取内容。无论您是在构建搜索工具、聚合器还是监控系统,Olostep Batches都能简化工作。 想要只提取结构化数据?使用Parsers获取您需要的字段。需要帮助?请联系info@olostep.com获取支持,或让我们为您的用例编写自定义脚本。