基於python編寫的微博應用

2022-09-29 13:24:14 字數 1803 閱讀 9202

在編寫自己的微博應用之前,先要到weibo開放平台申請應用的公鑰和私鑰。

**python版的sdk,開啟example目錄,仿照oauthsettokenupdate.py進行編碼,

複製** **如下:

# -*- coding: utf-8 -*-

from weibopy.auth import oauthhandler

from weibopy.api import api

consumer_key= '應用的key'

consumer_secret ='應用的app secret'

auth = oauthhandler(consumer_key, consumer_secret)

auth_url = auth.get_authorization_url()

print '程式設計客棧please authorize: ' + auth_url

verifier = raw_input('pin: ').strip()

auth.get_access_token(verifier)

api = api(auth)

status = api.update_status(status='hello world', lat='12.3', long='45.6') # 注意status必須是utf-8編碼的字串,經緯度是可以不寫的

print status.id

print status.text

執行這個程式就會提示乙個url鏈結,在瀏覽器裡開啟這個鏈結,並且給予訪問許可權,就會拿到一串pin碼。把這個pin碼輸上去,就會傳送一條推了,並且還會顯示使用者的access token key和access token secret。可以看到整個過程是很簡單的:

用你的consumer_key和cwww.cppcns.comonsumer_secret建立乙個oauthhandler物件auth。

告訴使用者訪問auth.get_authorization_url(),並讓使用者授權這個應用。

拿到用程式設計客棧戶的pin碼,用auth.get_access_token()拿到使用者的access token key和access token secret。

用auth建立乙個api物件api。

呼叫api的方法,例如update_status()就是發推。詳細說明可以看api文件。

當然,每次都要求使用者輸入pin碼也太傻了,所以在用auth.get_access_token()拿到access token key和access token secret之後,就可以直接用它們來建立api物件了:

複製** **如下:

# -*- coding: utf-8 -*-

from weibopy.auth import oauthhandler

from weibopy.api import api

consumer_key= '應用的key'

consumer_secret ='應用的app secret'

token = '使用者的access token key'

tokensecret = '使用者的access token secret'

auth = oauthhandler(consumer_key, consumer_secret)

auth.settoken(token, tokensecret)

api = api(auth)

status = api.update_status(status='搞定收工~')

本文標題: 基於python編寫的微博應用

本文位址: /jiaoben/python/115015.html

基於PECL OAuth打造微博應用

最近,國內主要門戶 相繼開放了微博平台,對開發者而言這無疑是個利好訊息,不過在實際使用中卻發現平台質量良莠不齊,有很多不完善的地方,就拿php版sdk來說吧,多半都是用twitteroauth 改的,一旦多平台整合,很容易出現命名衝突之類的問題。既然官方sdk不給力,那我們只能發揚自力更生的革命精神...

微博爬蟲python 微博爬蟲 python

本文爬取的是m站的微博內容,基於python 2.7 一 微博內容爬取 1.要爬取的微博首頁 2.手機微博是看不到翻頁,是一直往下載入的,但是其json格式的資料仍然以翻頁的形式呈現。3.開啟開發者工具,向下翻頁面,可以在network下的xhr的響應檔案中,找到json檔案的 如 通過分析發現每個...

基於Redis的微博的註冊

基於redis的微博的註冊 redis在網際網路公司中是必選的技術,因為網際網路公司的系統天生就是高併發特徵。但是能把redis運用的最好的就屬微博了。正因為redis的廣泛應用,使得微博能夠快速支撐日活躍使用者超2億,每日訪問量百億級,歷史資料高達千億級。微博線上規模,100t 儲存,1000 臺...