from weibopy import WeiboOauth2, WeiboClient from collections import defaultdict import time import webbrowser import re from pyecharts import Map from snownlp import SnowNLP from collections import Counter #————————————————————part 1 獲取微博訪問權限———————————————————— client_key = '' # 你的 app key client_secret = '' # 你的 app secret redirect_url = 'https://api.weibo.com/oauth2/default.html' auth = WeiboOauth2(client_key, client_secret, redirect_url) # 獲取認證 code webbrowser.open_new(auth.authorize_url) # 在打開的瀏覽器中完成操作 # 最終會跳轉到一個顯示 「微博 OAuth2.0」字樣的頁面 # 從這個頁面的 URL 中復制 code= 后的字符串 # URL 類似這樣 https://api.weibo.com/oauth2/default.html?code=9c88ff5051d273522700a6b0261f21e6 code = input('輸入 code:') # 使用 code 獲取 token token = auth.auth_access(code) # print(token) # AccessToken是為了避免多次調用API時需要重復輸入用戶名和密碼的一種設計。程序登錄后獲得AccessToken,使用AccessToken就能在一定時間內免密碼使用平台的功能。 #————————————————————part 2 調用 API 接口獲取數據———————————————————— # token 是剛剛獲得的 token,可以一直使用 client = WeiboClient(token['access_token']) # suffix 指定 API 的名稱,parmas 是參數,在文檔中有詳細描述 result = client.get(suffix='comments/show.json', params={'id': 4359907132320154, 'count': 200, 'page': 1}) #團團點名視覺中國 # print(result) #————————————————————part 3 整理所需數據———————————————————— province_list = defaultdict(list) # 保存按省划分的評論正文 comment_text_list = [] # 保存所有評論正文 # 獲取「團團點名視覺中國」評論列表 # 共獲取 10 頁 * 每頁最多 200 條評論 for i in range(1, 11): result = client.get(suffix='comments/show.json', params={'id': 4359907132320154, 'count': 200, 'page': i}) comments = result['comments'] if not len(comments): break for comment in comments: text = re.sub('回復.*?:', '', str(comment['text'])) province = comment['user']['province'] province_list[province].append(text) comment_text_list.append(text) print('已抓取評論 {} 條'.format(len(comment_text_list))) time.sleep(1) # ————————————————————part 4制作積極份子熱圖和表情統計———————————————————— # 獲取省份列表 provinces = {} results = client.get(suffix='common/get_province.json', params={'country': '001'}) for prov in results: for code, name in prov.items(): provinces[code] = name print(provinces) # 評論情感分析 positives = {} for province_code, comments in province_list.items(): sentiment_list = [] for text in comments: s = SnowNLP(text) sentiment_list.append(s.sentiments) # 統計平均情感 positive_number = sum(sentiment_list) positive = positive_number / len(sentiment_list) * 100 # 按省保存數據, 0010 為國家前綴 province_code = '0010' + str(province_code) if province_code in provinces: provice_name = provinces[province_code] positives[provice_name] = int(positive) # 繪制情感分布圖 keys = list(positives.keys()) values = list(positives.values()) map = Map("團團點名視覺中國 積極份子分析地域圖", width=1200, height=600) map.add("積極份子", keys, values, visual_range=[0, 100], maptype='china', is_visualmap=True, is_label_show=True, visual_text_color='#000') map.render(path="積極份子分布.html") # 獲取評論中出現的表情 emoji_list = [] for comment in comment_text_list: emojis = re.findall(re.compile(u'(\[.*?\])', re.S), comment) if emojis: for emoji in emojis: emoji_list.append(emoji) emoji_dict = Counter(emoji_list) print(emoji_dict)
效果圖:

附上微博來源地址(團團點名視覺中國):https://m.weibo.cn/detail/4359907132320154
