Skip to main content

一个免登陆获取新浪微博数据的Python库,简单易用

Project description

Weibo API SDK

一个免登陆获取新浪微博数据的Python库,简单易用

PyPI version Python Version License Downloads Tests

特性

  • 🚀 简单易用的 API 接口
  • 📦 免登陆获取微博数据
  • 🎯 支持获取用户信息、微博列表、文章、粉丝、关注等
  • 🐍 纯 Python 3.8+ 实现
  • 📝 完整的代码示例
  • ✅ 完善的单元测试

安装

从 PyPI 安装(推荐)

pip install weibo-api-sdk

从源码安装

git clone https://github.com/shibing624/weibo-api-sdk.git
cd weibo-api-sdk
pip install -e .

开发模式安装

pip install -e ".[dev]"  # 包含开发工具

快速开始

⚠️ 重要提示

由于微博的反爬虫机制,必须提供 Cookie 才能正常访问 API。

配置步骤

  1. 复制配置文件
cp .env.example .env
  1. 获取 Cookie

    • 访问 https://m.weibo.cn 并登录
    • F12 打开开发者工具
    • 切换到 Network 标签,刷新页面
    • 找到任意请求,复制 Cookie
  2. 编辑 .env 文件

WEIBO_COOKIE=your_actual_cookie_string_here

详细教程请参见:HOW_TO_GET_COOKIE.md

代码示例

from weibo_api_sdk import WeiboClient
from dotenv import load_dotenv
import os

# 加载 .env 文件中的配置
load_dotenv()

# 创建客户端(会自动读取 WEIBO_COOKIE 环境变量)
cookie = os.getenv('WEIBO_COOKIE')
client = WeiboClient(cookie=cookie)

# 获取用户信息
user = client.people('5623741644')  # 用户ID
print(f"用户名: {user.name}")
print(f"用户简介: {user.description}")
print(f"关注数: {user.follow_count}")
print(f"粉丝数: {user.followers_count}")

# 获取用户最新微博
print("\n最近发布的微博:")
for status in user.statuses.page(1):
    print(f"微博ID: {status.id}")
    print(f"发布时间: {status.created_at}")
    print(f"内容: {status.text}")
    print(f"点赞数: {status.attitudes_count}")
    print(f"评论数: {status.comments_count}")
    print(f"转发数: {status.reposts_count}")
    print("-" * 50)

# 获取粉丝列表
print("\n粉丝列表:")
for follower in user.followers.page(1):
    print(f"- {follower.name}")

# 获取关注列表
print("\n关注列表:")
for follow in user.follows.page(1):
    print(f"- {follow.name}")

# 获取文章列表
print("\n文章列表:")
for article in user.articles.page(1):
    print(f"- {article.text}")

示例代码

更多详细示例请查看 examples 目录:

运行示例

  1. 配置 Cookie(参见上方配置步骤)
  2. 运行示例:
python examples/basic_usage.py

output:

注意:示例代码会自动从 .env 文件读取 Cookie 配置。

API 文档

WeiboClient

主要的客户端类,用于创建各种微博对象。

方法

  • people(uid) - 获取用户信息
  • status(status_id) - 获取微博详情
  • statuses(uid) - 获取用户全部微博列表
  • origin_statuses(uid) - 获取用户原创微博列表
  • article(article_id) - 获取文章详情
  • articles(uid) - 获取用户文章列表
  • followers(uid) - 获取粉丝列表
  • follow(uid) - 获取关注列表

People 对象

用户对象,包含用户的基本信息和相关数据。

属性

  • id - 用户ID
  • name - 昵称
  • description - 简介
  • gender - 性别
  • avatar - 头像URL
  • followers_count - 粉丝数
  • follow_count - 关注数
  • statuses - 微博列表
  • articles - 文章列表
  • followers - 粉丝列表
  • follows - 关注列表

Status 对象

微博对象,包含微博的详细信息。

属性

  • id - 微博ID
  • text - 微博内容
  • created_at - 发布时间
  • source - 发布来源
  • attitudes_count - 点赞数
  • comments_count - 评论数
  • reposts_count - 转发数
  • user - 发布用户
  • pic_urls - 图片URL列表

注意事项

  • 请合理控制请求频率,避免对微博服务器造成过大压力
  • page(n) 方法用于获取指定页的数据
  • all() 方法会获取所有数据,对于数据量大的用户请谨慎使用
  • 所有 API 都是免登陆的,但受微博反爬虫机制限制

开发

环境要求

  • Python 3.8+
  • requests >= 2.10.0
  • python-dotenv >= 0.19.0

开发环境设置

# 克隆仓库
git clone https://github.com/shibing624/weibo-api-sdk.git
cd weibo-api-sdk

# 安装开发依赖
pip install -e ".[dev]"

# 或使用 requirements.txt
pip install -r requirements.txt

TODO

  • 搜索接口
  • 根据用户昵称创建用户
  • 头条文章获取优化
  • 文章评论功能
  • 完善文档
  • 添加单元测试
  • 添加更多示例
  • 增加测试覆盖率到 90%+
  • 添加 GitHub Actions CI/CD

联系方式

许可证

本项目采用 MIT 许可证

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_api_sdk-0.1.2.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

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

weibo_api_sdk-0.1.2-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file weibo_api_sdk-0.1.2.tar.gz.

File metadata

  • Download URL: weibo_api_sdk-0.1.2.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for weibo_api_sdk-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d8f9a78e3645d6c31ff2105777111367ef05940f0a61006554e2ea9cfbfb0e7b
MD5 c83a238c8b6122e7fcf881526bea0a34
BLAKE2b-256 7429f984bedf2cdc00c4b9e19dd83cb4140574c4b6ea80d1427ceb75c9ccfdcf

See more details on using hashes here.

File details

Details for the file weibo_api_sdk-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: weibo_api_sdk-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for weibo_api_sdk-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 59c65174a26c0380c07ccb01248eb00656b06e6e35570f5bfc0e86f5971035c3
MD5 cd8b03232ef4b7edec78a3702518301c
BLAKE2b-256 5800aad60be4d66d8a93868f886c8485ff70ac9bb86652f4b6365987439b3e47

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