Weibo API SDK
一个免登陆获取新浪微博数据的Python库,简单易用
特性
- 🚀 简单易用的 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。
配置步骤
- 复制配置文件
cp .env.example .env
-
获取 Cookie
- 访问 https://m.weibo.cn 并登录
- 按
F12打开开发者工具 - 切换到
Network标签,刷新页面 - 找到任意请求,复制
Cookie值
-
编辑 .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 目录:
- basic_usage.py - 基本使用示例,包含所有核心功能
运行示例
- 配置 Cookie(参见上方配置步骤)
- 运行示例:
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- 用户IDname- 昵称description- 简介gender- 性别avatar- 头像URLfollowers_count- 粉丝数follow_count- 关注数statuses- 微博列表articles- 文章列表followers- 粉丝列表follows- 关注列表
Status 对象
微博对象,包含微博的详细信息。
属性
id- 微博IDtext- 微博内容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
联系方式
- GitHub Issues: 报告问题或功能请求
- 邮箱: xuming624@qq.com
- 微信: xuming624(备注:姓名-公司-Weibo)
许可证
本项目采用 MIT 许可证
Metadata
Release files for weibo-api-sdk 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| weibo_api_sdk-0.1.2.tar.gz | 27.9 kB | Without provenance | Details for weibo_api_sdk-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| weibo_api_sdk-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for weibo_api_sdk-0.1.2-py3-none-any.whl |
Total release size: 48.1 kB
Release files / weibo_api_sdk-0.1.2.tar.gz
| Download URL | weibo_api_sdk-0.1.2.tar.gz |
|---|---|
| Size | 27.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d8f9a78e3645d6c31ff2105777111367ef05940f0a61006554e2ea9cfbfb0e7b
|
|
BLAKE2b-256 checksum How to use checksums |
7429f984bedf2cdc00c4b9e19dd83cb4140574c4b6ea80d1427ceb75c9ccfdcf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.5
|
Release files / weibo_api_sdk-0.1.2-py3-none-any.whl
| Download URL | weibo_api_sdk-0.1.2-py3-none-any.whl |
|---|---|
| Size | 20.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
59c65174a26c0380c07ccb01248eb00656b06e6e35570f5bfc0e86f5971035c3
|
|
BLAKE2b-256 checksum How to use checksums |
5800aad60be4d66d8a93868f886c8485ff70ac9bb86652f4b6365987439b3e47
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.5
|
Security for weibo-api-sdk 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.