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.5.dev0.tar.gz (13.8 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.5.dev0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spidermanager_sdk-0.1.5.dev0.tar.gz
  • Upload date:
  • Size: 13.8 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.5.dev0.tar.gz
Algorithm Hash digest
SHA256 8ff71e30fd7864a85a304dfbb503ae166236e134c89bb37badb876fb47cb1ab6
MD5 cc68991aeba97206ec9ff9f4e3065f0c
BLAKE2b-256 16164043ed3de6a911a3a2b2d82ef2c40f513e5c04f67af63c9bcecb759ce812

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for spidermanager_sdk-0.1.5.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2381f69600190364fa98a320fa2e0bcaecef917d3ecc355dda3fa83c5f8c130
MD5 559bde39d24d184f2e6afc2a7036f7f8
BLAKE2b-256 0c10af8651ff59ecd2f2f1d996d12e7787cec687d0f8f7eec37084d556319dfa

See more details on using hashes here.

Provenance

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