生活随笔
收集整理的這篇文章主要介紹了
微博API接入初识【cxn专用】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
微博API官方文檔
本文介紹
環境
- Windows
- Python 3.8.10
- sinaweibopy3-1.3 (pip3 install sinaweibopy3)
- requests
成為微博開發者
微博官方新手教程 (cxn可以跳過,用博主的即可)
- 完善身份信息并認證
- 創建微鏈接
- 填寫授權回調頁
- 注:https://api.weibo.com/oauth2/default.html
通過鑒權
- 以下只為獲取access_token,采用的是weiboSDK
from weibo
import APIClient
import webbrowserAPP_KEY
= 'xxx'
APP_SECRET
= 'xxx'
CALLBACK_URL
= 'https://api.weibo.com/oauth2/default.html'
client
= APIClient
(app_key
=APP_KEY
, app_secret
=APP_SECRET
, redirect_uri
=CALLBACK_URL
)
url
= client
.get_authorize_url
()
print(url
)
webbrowser
.open_new
(url
)
print('輸入url中code后面的內容后按回車鍵:')
code
= input()
r
= client
.request_access_token
(code
)
access_token
= r
.access_token
expires_in
= r
.expires_in
print("access_token, expires_in:", access_token
, expires_in
)
client
.set_access_token
(access_token
, expires_in
)
uid
= client
.get
.account__get_uid
()["uid"]
print(uid
)
獲取單條微博內容
import json
import requestsaccess_token
= "xxx"
res
= requests
.get
(f"https://api.weibo.com/2/statuses/user_timeline.json?access_token={access_token}")weibo_list_json
= json
.loads
(res
.content
)
for weibo_dict
in weibo_list_json
["statuses"]:weibo_id
= weibo_dict
["id"]print(weibo_id
)res
= requests
.get
(f"https://api.weibo.com/2/statuses/show.json?access_token={access_token}&id={weibo_id}")weibo_json
= json
.loads
(res
.content
)print(weibo_json
["reposts_count"])
總結
以上是生活随笔為你收集整理的微博API接入初识【cxn专用】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。