欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python使用新浪微博API发送微博的例子

时间:2021-02-25 10:38:23|栏目:Python代码|点击:

1、注册一个新浪应用,得到appkey和secret,以及token,将这些信息写入配置文件sina_weibo_config.ini,内容如下,仅举例:

复制代码 代码如下:

[userinfo]
CONSUMER_KEY=8888888888
CONSUMER_SECRET=777777f3feab026050df37d711200000
TOKEN=2a21b19910af7a4b1962ad6ef9999999
TOKEN_SECRET=47e2fdb0b0ac983241b0caaf45555555


2、调用新浪微博的Open Api,编码:
复制代码 代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from weibopy.auth import OAuthHandler
from weibopy.api import API
import ConfigParser

def press_sina_weibo():
    '''
    调用新浪微博Open Api实现通过命令行写博文,功能有待完善
    author: socrates
    date:2012-02-06
    新浪微博:@没耳朵的羊
    '''
    sina_weibo_config = ConfigParser.ConfigParser()
    #读取appkey相关配置文件
    try:
        sina_weibo_config.readfp(open('sina_weibo_config.ini'))
    except ConfigParser.Error:
        print 'read sina_weibo_config.ini failed.'

    #获取需要的信息
    consumer_key = sina_weibo_config.get("userinfo","CONSUMER_KEY")
    consumer_secret =sina_weibo_config.get("userinfo","CONSUMER_SECRET")
    token = sina_weibo_config.get("userinfo","TOKEN")
    token_sercet = sina_weibo_config.get("userinfo","TOKEN_SECRET")

    #调用新浪微博OpenApi(python版)
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.setToken(token, token_sercet)
    api = API(auth)

    #通过命令行输入要发布的内容
    weibo_content = raw_input('Please input content:')
    status = api.update_status(status=weibo_content)
    print "Press sina weibo successful, content is: %s" % status.text

if __name__ == '__main__':
    press_sina_weibo()


3、 运行效果:

命令行输入:
4、微博发送成功效果:

 

上一篇:windows下cx_Freeze生成Python可执行程序的详细步骤

栏    目:Python代码

下一篇:Python Flask-web表单使用详解

本文标题:Python使用新浪微博API发送微博的例子

本文地址:http://www.codeinn.net/misctech/69715.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有