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


Posted in Python onApril 10, 2014

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、 运行效果:

命令行输入:Python使用新浪微博API发送微博的例子
4、微博发送成功效果:

 
Python 相关文章推荐
Pyhton中防止SQL注入的方法
Feb 05 Python
python学习之第三方包安装方法(两种方法)
Jul 30 Python
python 线程的暂停, 恢复, 退出详解及实例
Dec 06 Python
python 3.6.2 安装配置方法图文教程
Sep 18 Python
Python安装Flask环境及简单应用示例
May 03 Python
python pip源配置,pip配置文件存放位置的方法
Jul 12 Python
在SQLite-Python中实现返回、查询中文字段的方法
Jul 17 Python
pygame实现五子棋游戏
Oct 29 Python
如何分离django中的媒体、静态文件和网页
Nov 12 Python
基于TensorFlow常量、序列以及随机值生成实例
Jan 04 Python
python实现在线翻译功能
Mar 03 Python
拒绝盗图!教你怎么用python给图片加水印
Jun 04 Python
一个检测OpenSSL心脏出血漏洞的Python脚本分享
Apr 10 #Python
Python删除指定目录下过期文件的2个脚本分享
Apr 10 #Python
python实现随机密码字典生成器示例
Apr 09 #Python
Python下的Mysql模块MySQLdb安装详解
Apr 09 #Python
使用python实现递归版汉诺塔示例(汉诺塔递归算法)
Apr 08 #Python
python计算圆周长、面积、球体体积并画出圆
Apr 08 #Python
python实现类似ftp传输文件的网络程序示例
Apr 08 #Python
You might like
php中的strpos使用示例
2014/02/27 PHP
php单链表实现代码分享
2016/07/04 PHP
Ajax实现对静态页面的文章访问统计功能示例
2016/10/10 PHP
php实现的读取CSV文件函数示例
2017/02/07 PHP
基于jquery的表头固定的若干方法
2011/01/27 Javascript
jquery表格内容筛选实现思路及代码
2013/04/16 Javascript
使用js检测浏览器的实现代码
2013/05/14 Javascript
自动刷新网页,自动刷新当前页面,JS调用
2013/06/24 Javascript
Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
2015/01/01 NodeJs
js实现向右横向滑出的二级菜单效果
2015/08/27 Javascript
AngularJs 60分钟入门基础教程
2016/04/03 Javascript
详解Jquery 遍历数组之$().each方法与$.each()方法介绍
2017/01/09 Javascript
JS+CSS实现下拉刷新/上拉加载插件
2017/03/31 Javascript
浅谈Angular2 模块懒加载的方法
2017/10/04 Javascript
jQuery实现的淡入淡出与滑入滑出效果示例
2018/04/18 jQuery
[01:02:03]2014 DOTA2华西杯精英邀请赛 5 24 NewBee VS VG
2014/05/26 DOTA
python3爬取各类天气信息
2018/02/24 Python
点球小游戏python脚本
2018/05/22 Python
python dataframe常见操作方法:实现取行、列、切片、统计特征值
2018/06/09 Python
OPENCV去除小连通区域,去除孔洞的实例讲解
2018/06/21 Python
tensorflow实现加载mnist数据集
2018/09/08 Python
Python实现变声器功能(萝莉音御姐音)
2019/12/05 Python
python3实现raspberry pi(树莓派)4驱小车控制程序
2020/02/12 Python
python连接mongodb集群方法详解
2020/02/13 Python
python如何删除列为空的行
2020/07/17 Python
5个你不知道的HTML5的接口介绍
2013/08/07 HTML / CSS
html5本地存储 localStorage操作使用详解
2016/09/20 HTML / CSS
英国最大的体育&时尚零售公司:JD Sports
2017/12/13 全球购物
品管员岗位职责
2013/11/10 职场文书
管理科学大学生求职信
2013/11/13 职场文书
好家长事迹材料
2014/01/23 职场文书
入党宣誓仪式主持词
2015/06/29 职场文书
嘉年华活动新闻稿
2015/07/17 职场文书
幼儿园教师暑期培训心得体会
2016/01/09 职场文书
行政后勤人员工作计划应该怎么写?
2019/08/16 职场文书
小学生作文之《压岁钱的烦恼》
2019/09/27 职场文书