Python使用新浪微博API發(fā)送微博的例子
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實現(xiàn)通過命令行寫博文,功能有待完善
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)
#通過命令行輸入要發(fā)布的內容
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ā)送成功效果:
![]() |
相關文章
安裝pycurl報錯Could not run curl-config: &ap
這篇文章主要為大家介紹了安裝pycurl報錯Could not run curl-config: 'curl-config'解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12Pandas中datetime數(shù)據(jù)類型的使用
本文主要介紹了Pandas中datetime數(shù)據(jù)類型的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-12-12Python semaphore evevt生產(chǎn)者消費者模型原理解析
這篇文章主要介紹了Python semaphore evevt生產(chǎn)者消費者模型原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03Python日期與時間模塊(datetime+time+Calendar+dateuil?)相關使用講解
這篇文章主要介紹了Python日期與時間模塊(datetime+time+Calendar+dateuil?)相關使用講解,文章圍繞主題展開詳細的內容戒殺,具有一定的參考價值,需要的朋友可以參考一下2022-09-09