极简 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
977c01731b90de5b7332b645d55f7d8c73dbfe732affe71382a3532b7e2ef6c9
|
|
| MD5 |
32f02124f1cb224ba4d804629ab3f8f0
|
|
| BLAKE2b-256 |
9f706ef9eb0905d625a6d6f4ffeab065864d3a52a19561046ea7552929fe4564
|
Provenance
The following attestation bundles were made for spidermanager_sdk-0.1.4.dev0.tar.gz:
Publisher:
publish.yml on bjargo/SpiderManager-SDK
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spidermanager_sdk-0.1.4.dev0.tar.gz -
Subject digest:
977c01731b90de5b7332b645d55f7d8c73dbfe732affe71382a3532b7e2ef6c9 - Sigstore transparency entry: 1039517470
- Sigstore integration time:
-
Permalink:
bjargo/SpiderManager-SDK@5b7006393c429906b6e92f28acd25892a5360e0a -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/bjargo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5b7006393c429906b6e92f28acd25892a5360e0a -
Trigger Event:
push
-
Statement type:
File details
Details for the file spidermanager_sdk-0.1.4.dev0-py3-none-any.whl.
File metadata
- Download URL: spidermanager_sdk-0.1.4.dev0-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f8894dd031d13f3d123da2b34b8419b0035b9523ea083b76df7858c83f8f7ba
|
|
| MD5 |
c2442a30415fcd04dd42851be72816a6
|
|
| BLAKE2b-256 |
11a612553c5ef3d2344d24d58913a91f8d0e23bac10c4cce324ae390872a3283
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spidermanager_sdk-0.1.4.dev0-py3-none-any.whl -
Subject digest:
6f8894dd031d13f3d123da2b34b8419b0035b9523ea083b76df7858c83f8f7ba - Sigstore transparency entry: 1039517546
- Sigstore integration time:
-
Permalink:
bjargo/SpiderManager-SDK@5b7006393c429906b6e92f28acd25892a5360e0a -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/bjargo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5b7006393c429906b6e92f28acd25892a5360e0a -
Trigger Event:
push
-
Statement type: