Skip to main content

极简 Python SDK,将爬虫采集数据通过 HTTP 异步中转至 SpiderManager 后端

Project description

SpiderManager SDK

极简 Python SDK,将爬虫采集数据通过 HTTP 异步中转至 SpiderManager 后端,实现爬虫逻辑与数据存储的完全解耦。

特性

  • 零配置启动:自动从环境变量 TASK_ID / SPIDER_API_URL 读取配置
  • 异步缓冲:内存 buffer 按阈值(20条)或时间窗口(3秒)批量上报,减少网络 IO
  • 容器安全:通过 atexit + SIGTERM 双保险,确保 Docker 销毁前 flush 全部数据
  • 最小依赖:仅依赖 httpx,不侵入爬虫业务代码

安装

pip install spidermanager-sdk

快速开始

from spidermanager_sdk import sdk

# 初始化(容器中自动读取环境变量,本地开发可手动指定)
sdk.init(api_url="http://localhost:8000", task_id="task-001")

# 上报数据(会自动缓冲、批量上报)
sdk.insert("articles", {"title": "Hello", "url": "https://example.com"})

# 批量上报
sdk.insert("articles", [
    {"title": "A", "url": "https://a.com"},
    {"title": "B", "url": "https://b.com"},
])

# 程序结束时自动 flush,也可手动触发
sdk.flush()

配置选项

参数 环境变量 默认值 说明
api_url SPIDER_API_URL 后端地址
task_id TASK_ID 任务 ID
buffer_size 20 缓冲条数阈值
flush_interval 3.0 时间窗口(秒)

异步 API (Asyncio)

由于爬虫开发经常使用 httpx, aiohttp, Playwright 等异步工具,SDK 也提供了原生的 Async 接口。 推荐使用 async with 上下文管理器,离开上下文时将自动触发 flush。

import asyncio
from spidermanager_sdk.aio import async_sdk

async def main():
    # 自动读取环境变量并初始化,退出时自动 flush
    async with async_sdk:
        await async_sdk.insert("articles", {"title": "Async Data", "url": "https://a.com"})
        
        # 批量插入
        await async_sdk.insert("articles", [
            {"title": "B", "url": "https://b.com"},
            {"title": "C", "url": "https://c.com"},
        ])

if __name__ == "__main__":
    asyncio.run(main())

架构

sdk.insert()
    ↓
FlushBuffer (内存缓冲, 线程安全)
    ↓  条数阈值 / 定时器触发
HttpTransport.send_batch()
    ↓
POST /api/v1/tasks/data/ingest?task_id=xxx

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spidermanager_sdk-0.1.3.dev0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spidermanager_sdk-0.1.3.dev0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file spidermanager_sdk-0.1.3.dev0.tar.gz.

File metadata

  • Download URL: spidermanager_sdk-0.1.3.dev0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spidermanager_sdk-0.1.3.dev0.tar.gz
Algorithm Hash digest
SHA256 ff34e0ef44665947dbd6bfc3b30ee3f0de136e88c12327f681741cd885194fbc
MD5 53d057db1ff4143f8ab7b6d4c7de55d1
BLAKE2b-256 ebc74dfa642045677936b530b80cde39eb24e3576c4a6e5a907a1f719da76ed0

See more details on using hashes here.

Provenance

The following attestation bundles were made for spidermanager_sdk-0.1.3.dev0.tar.gz:

Publisher: publish.yml on bjargo/SpiderManager-SDK

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spidermanager_sdk-0.1.3.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for spidermanager_sdk-0.1.3.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7e2bb10a2b9c38154c8d717bab45218c0035d416a5a7362abbc3d371c486011
MD5 fff242aee54a598df5c3365a3d9ad06b
BLAKE2b-256 26d5671c55fc1dce3b72994fcdf99550893bf31a3b34b14586fa3eb3b78b09a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for spidermanager_sdk-0.1.3.dev0-py3-none-any.whl:

Publisher: publish.yml on bjargo/SpiderManager-SDK

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page