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

yipeiwu_com6年前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、微博发送成功效果:

 

相关文章

opencv3/C++实现视频背景去除建模(BSM)

opencv3/C++实现视频背景去除建模(BSM)

视频背景建模主要使用到: 高斯混合模型(Mixture Of Gauss,MOG) createBackgroundSubtractorMOG2(int history=500, d...

python 3.0 模拟用户登录功能并实现三次错误锁定

Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。 Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。 像Perl...

web.py 十分钟创建简易博客实现代码

一、web.py简介 web.py是一款轻量级的Python web开发框架,简单、高效、学习成本低,特别适合作为python web开发的入门框架。官方站点:http://webpy....

对python数据清洗容易遇到的函数-re.sub bytes string详解

re.sub 功能,比replace强大的替换函数,将正则表达式匹配上的模块替换成repl re.sub(pattern, repl, string, count=0, flags=0)...

Python中的super用法详解

一、问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1: 代码段1: 复制代码 代码如下:  c...