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/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.4.dev0.tar.gz (12.0 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.4.dev0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spidermanager_sdk-0.1.4.dev0.tar.gz
  • Upload date:
  • Size: 12.0 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.4.dev0.tar.gz
Algorithm Hash digest
SHA256 977c01731b90de5b7332b645d55f7d8c73dbfe732affe71382a3532b7e2ef6c9
MD5 32f02124f1cb224ba4d804629ab3f8f0
BLAKE2b-256 9f706ef9eb0905d625a6d6f4ffeab065864d3a52a19561046ea7552929fe4564

See more details on using hashes here.

Provenance

The following attestation bundles were made for spidermanager_sdk-0.1.4.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.4.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for spidermanager_sdk-0.1.4.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f8894dd031d13f3d123da2b34b8419b0035b9523ea083b76df7858c83f8f7ba
MD5 c2442a30415fcd04dd42851be72816a6
BLAKE2b-256 11a612553c5ef3d2344d24d58913a91f8d0e23bac10c4cce324ae390872a3283

See more details on using hashes here.

Provenance

The following attestation bundles were made for spidermanager_sdk-0.1.4.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