Skip to main content
http://img.shields.io/pypi/v/weibo.svg?style=flat http://img.shields.io/pypi/dm/weibo.svg?style=flat

新浪微博 python SDK.

使用 requests 进行相关网络操作,因而相对于官方的 sdk,极大的简化了代码,并提高了可读性。

安装

推荐使用 pip 安装。

$ pip install weibo

使用说明

准备工作

  • 首先,注册一个新浪开发者帐号,并在 新浪开发平台 新建一个 app.

  • 创建好应用之后在 ‘应用信息 -> ‘基本信息’ 里面获取 App Key 和 App Secret.

  • 在 ‘应用信息 -> ‘高级信息’ 里面设置好 ‘授权回调页’.

示例

下面分别用 API_KEY, API_SECRET, REDIRECT_URI 代表准备工作里面的三个参数。

Token 认证

>>> from weibo import Client
>>> c = Client(API_KEY, API_SECRET, REDIRECT_URI)
>>> c.authorize_url
'https://api.weibo.com/oauth2/authorize?redirect_uri=http%3A%2F%2F127.0.0.1%2Fcallback&client_id=123456'

复制链接到浏览器打开,获取 code.

>>> c.set_code('abcdefghijklmn')

client 初始化完成。token 可以被保存下来供下次调用时直接使用。

>>> token = c.token
>>> c2 = Client(API_KEY, API_SECRET, REDIRECT_URI, token)
>>> c2.get('users/show', uid=2703275934)

帐号认证

除了使用 token 认证,还可以使用 username / password 进行认证。

>>> from weibo import Client
>>> c = Client(API_KEY, API_SECRET, REDIRECT_URI, username='admin', password='secret')
>>> c.get('users/show', uid=1282440983)

接口调用

参考 微博开发文档 进行接口调用。

>>> c.get('users/show', uid=1282440983)
>>> c.post('statuses/update', status='python sdk test, check out http://lxyu.github.io/weibo/')

client 兼容上传图片接口。

>>> f = open('avatar.png', 'rb')
>>> c.post('statuses/upload', status='new avatar!', pic=f)

Metadata

Release files for weibo 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for weibo 0.2.2
File Size Uploaded
weibo-0.2.2.tar.gz 3.6 kB Details

Release files / weibo-0.2.2.tar.gz

Download URL weibo-0.2.2.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3f44367192dce0490b385ab2f7c49399f138d3d11621f294f2fd63b1486b0270
BLAKE2b-256 checksum
How to use checksums
553763359f39566e8444c0b037248a8707c6a070239c68d7fd30b94731136e88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.2 This release

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page