Skip to main content

微博 API 的纯异步客户端

Project description

weibo-cli

简洁的微博 API 异步客户端。类型安全,自动认证,开箱即用。

安装

uv pip install -e .

快速开始

import asyncio
from weibo_cli import WeiboClient

async def main():
    async with WeiboClient() as client:
        # 获取用户信息
        user = await client.get_user("1749127163")
        print(f"用户: {user.screen_name}")

        # 获取用户微博
        posts = await client.get_user_posts("1749127163", page=1)
        print(f"微博数: {len(posts)}")

        # 获取微博详情
        post = await client.get_post("5226761046462968")
        print(f"内容: {post.text}")

        # 获取评论
        comments = await client.get_post_comments("5226761046462968")
        print(f"评论数: {len(comments)}")

asyncio.run(main())

API

WeiboClient

异步上下文管理器,自动处理连接生命周期。

async with WeiboClient(
    cookies=None,
    config=None,
    logger=None,
    max_concurrent_requests=4,
    requests_per_interval=10,
    rate_interval_seconds=1.0,
) as client:
    ...

参数

  • cookies: 可选的 Cookie 字符串
  • config: WeiboConfig 实例,默认使用标准配置
  • logger: 自定义 logger,默认使用模块 logger
  • max_concurrent_requests: 并发请求上限
  • requests_per_interval: 每时间窗口的请求数上限
  • rate_interval_seconds: 速率限制窗口(秒)
  • rate_limiter: 传入自定义 RateLimiter,覆盖默认策略

方法

await get_user(user_id: str) -> User

获取用户资料。

await get_user_posts(user_id: str, page: int = 1) -> list[Post]

获取用户微博时间线。

await get_post(post_id: str) -> Post

获取微博详情(移动端 API)。

await get_post_comments(post_id: str) -> list[Comment]

获取微博评论。

WeiboConfig

配置类,控制 HTTP、认证、API 行为。

from weibo_cli import WeiboConfig

# 默认配置
config = WeiboConfig()

# 快速配置(低延迟,低重试)
config = WeiboConfig.create_fast()

# 保守配置(高超时,高重试)
config = WeiboConfig.create_conservative()

配置字段

@dataclass
class WeiboConfig:
    http: HttpConfig       # HTTP 配置
    auth: AuthConfig       # 认证配置
    api: ApiConfig         # API 端点配置

HttpConfig

timeout: float = 10.0                    # 请求超时(秒)
max_retries: int = 3                     # 最大重试次数
base_delay: float = 1.0                  # 基础延迟(秒)
max_delay: float = 60.0                  # 最大延迟(秒)
max_connections: int = 20                # 最大连接数
max_keepalive_connections: int = 5       # 保持活跃连接数

AuthConfig

cookie_ttl: float = 300.0                # Cookie 缓存时间(秒)

ApiConfig

base_url: str = "https://weibo.com"
mobile_url: str = "https://m.weibo.cn"
user_agent: str = "Mozilla/5.0 ..."

数据模型

所有模型都基于 Pydantic v2BaseModel,自动完成类型转换、校验与序列化。

User

from weibo_cli.models import User

user = User(
    id=123,
    screen_name="Test User",
    profile_image_url="https://example.com/avatar.jpg",
    followers_count=100,
)

# Pydantic API 同样可用
user_dict = user.model_dump()

Post

from datetime import datetime

from weibo_cli.models import Post, Image, Video

post = Post(
    id=1,
    created_at=datetime.utcnow(),
    text="Hello Weibo",
    user=user,
    images=[Image(id="pic1", thumbnail_url="...", large_url="...", original_url="...")],
    video=Video(duration=10.5, play_count=999),
    reposts_count=1,
    comments_count=2,
    attitudes_count=3,
)

# 每个模型都带有 `raw` 字段,保存原始 JSON 字符串快照
print(post.raw)

异常

from weibo_cli.exceptions import (
    WeiboError,      # 基础异常
    AuthError,       # 认证失败
    NetworkError,    # 网络错误
    ParseError,      # 解析错误
)

测试

uv run pytest -v

发布前清理

发布/打包前运行 scripts/prep_release.sh,脚本会:

  • 执行 python -m compileall -q src tests,提前暴露语法错误
  • 清理 __pycache__ 目录,确保工作区干净
bash scripts/prep_release.sh

验证 XSRF 修复

运行 python examples/verify_xsrf_fix.py 即可验证 CookieManager 是否成功获取 XSRF-TOKEN 并调用时间线接口:

python examples/verify_xsrf_fix.py

脚本会:

  • 输出生成的访客 Cookie 片段
  • 打印获取到的 XSRF-TOKEN,若缺失会直接报错
  • 调用 get_user_posts 并统计返回的微博数量,用于确认 HTTP 432 错误已消失
# 运行所有测试
uv run pytest -v

# 带覆盖率
uv run pytest --cov -v

架构

WeiboClient              # Facade 入口
├── HttpClient           # HTTP 请求
├── CookieManager        # Cookie 管理(自动获取访客 Cookie + XSRF token)
├── RetryStrategy        # 重试策略(指数退避)
├── UserParser           # 用户数据解析
├── PostParser           # 微博数据解析
└── CommentParser        # 评论数据解析

设计原则

  • 单一职责:每个类只做一件事
  • 依赖注入:组件可独立测试
  • 无全局状态:线程安全
  • 显式优于隐式:所有配置都可见

许可证

GNU Affero General Public License v3.0

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

weibo_cli-1.0.4.tar.gz (39.3 kB view details)

Uploaded Source

Built Distribution

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

weibo_cli-1.0.4-py3-none-any.whl (36.9 kB view details)

Uploaded Python 3

File details

Details for the file weibo_cli-1.0.4.tar.gz.

File metadata

  • Download URL: weibo_cli-1.0.4.tar.gz
  • Upload date:
  • Size: 39.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.15

File hashes

Hashes for weibo_cli-1.0.4.tar.gz
Algorithm Hash digest
SHA256 d1e3396aedd7ebe9ad8bdb2d9b445aea8934ac5c610face10a8616dbcae28f57
MD5 5fbd2813d45f49afcd5627bf18e1c143
BLAKE2b-256 6c0fa749c5859a303af22166d15e535090390c91cce6d6c80b586617dd246b60

See more details on using hashes here.

File details

Details for the file weibo_cli-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: weibo_cli-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 36.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.15

File hashes

Hashes for weibo_cli-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 10f29b579d70d90169794814c38e0db04b375c5f670b29ed591c56e1d40be755
MD5 bf3f9adb16e14896e436a659103278f1
BLAKE2b-256 f094176f25a5fe8d2cb32e63273c919094f69095a7b9ddde9b153ca368e84f5c

See more details on using hashes here.

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