国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用新浪微博API發(fā)送微博的例子

2020-02-23 05:16:54
字體:
供稿:網(wǎng)友

1、注冊一個新浪應(yīng)用,得到appkey和secret,以及token,將這些信息寫入配置文件sina_weibo_config.ini,內(nèi)容如下,僅舉例:

代碼如下:
[userinfo]
CONSUMER_KEY=8888888888
CONSUMER_SECRET=777777f3feab026050df37d711200000
TOKEN=2a21b19910af7a4b1962ad6ef9999999
TOKEN_SECRET=47e2fdb0b0ac983241b0caaf45555555


2、調(diào)用新浪微博的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():
    '''
    調(diào)用新浪微博Open Api實現(xiàn)通過命令行寫博文,功能有待完善
    author: socrates
    date:2012-02-06
    新浪微博:@沒耳朵的羊
    '''
    sina_weibo_config = ConfigParser.ConfigParser()
    #讀取appkey相關(guān)配置文件
    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")

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

    #通過命令行輸入要發(fā)布的內(nèi)容
    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、微博發(fā)送成功效果:

 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 翁源县| 乌什县| 象州县| 阿图什市| 和田市| 武川县| 中西区| 衡阳县| 石家庄市| 阿坝县| 交城县| 海伦市| 浙江省| 来安县| 孝感市| 涟水县| 天气| 闻喜县| 门源| 武定县| 阿拉善盟| 株洲市| 临海市| 罗山县| 玉树县| 扬州市| 绥中县| 乐安县| 绵竹市| 湟源县| 海门市| 集安市| 镇远县| 彭阳县| 和林格尔县| 于都县| 西丰县| 西丰县| 西乌珠穆沁旗| 阿拉善左旗| 上思县|