????????以前用過一次sinaweibopy3來獲取新浪的數(shù)據(jù)更卒,但是太麻煩了5确酢!蹂空!經(jīng)過研究之后俯萌,發(fā)現(xiàn)weibopy庫比sinaweibopy3方便很多果录,簡單易懂方便上手,短短幾行代碼就能搞定咐熙,于是決定分享出來弱恒。
環(huán)境:python3.7
準(zhǔn)備:pip install weibopy
???????????pip install webbrowser
網(wǎng)站:https://m.weibo.cn/
提前申請新浪微博的app_key和app_secret https://jingyan.baidu.com/article/375c8e19b8024125f2a22913.html
代碼整理如下:
#-*- coding:utf-8 -*-
# import sys
# reload(sys)
# sys.setdefaultencoding("utf-8")
from weibopy import WeiboOauth2, WeiboClient
import webbrowser
APP_KEY = '000000000'
APP_SECRET = '0000000000000xxxxxxxxxxxxxxxxxx'
CALLBACK_URL = 'https://api.weibo.com/oauth2/default.html'
#回調(diào)授權(quán)頁面,用戶完成授權(quán)后返回的頁面
client= WeiboOauth2(client_id=APP_KEY, client_secret=APP_SECRET, redirect_url=CALLBACK_URL)
#得到授權(quán)頁面的url棋恼,并打開這個url
url = client.authorize_url
webbrowser.open_new(url)
code = input("please input code : ")
r = client.auth_access(code)
b = WeiboClient(r['access_token'])
# 獲取評論信息
for i in range(1,10):
result = b.get(suffix='comments/show.json', params={'id': 4459137180328909, 'count': 200, 'page': i})
comments = result['comments']
for j in comments:
text = j["text"]
id = j["id"]
created_at = j["created_at"]
print('page:{}'.format(i),text,id,created_at)
運(yùn)行后效果如圖所示:
????????根據(jù)新浪comments/show文檔https://open.weibo.com/wiki/2/comments/show返弹,提取自己需要的數(shù)據(jù)。