python實現(xiàn) 獲取b站主播直播間 粉絲牌信息的方法
前言
用于實現(xiàn)通過牌子逆向查主播信息這個功能。
插件基于Nonebot2開發(fā),鏈接:https://github.com/Ikaros-521/nonebot_plugin_searchBiliInfo
工程下載
github:https://github.com/Ikaros-521/get_bili_medal_list
gitee:https://gitee.com/ikaros-521/get_bili_medal_list
目錄結(jié)構(gòu)
data.py數(shù)據(jù)源自vtbs.moe
1.py用于獲取數(shù)據(jù)
2.py用于中斷時候的下標檢索
data_medal.py用于存儲用戶結(jié)果數(shù)據(jù)
API
https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByUser?from=0¬_mock_enter_effect=1&room_id=
傳入主播的房間號,解析json["data"]["medal"]["up_medal"]["medal_name"]
,即可。
使用
安裝相應(yīng)的第三方庫(aiohttp)后,python 1.py
即可。
核心源碼
1.py
import json import asyncio import aiohttp import time from itertools import islice # data.py存儲著從vtbs.moe獲取的主播數(shù)據(jù) from data import DATA # data_medal.py用于存儲獲取的主播牌子信息 from data_medal import DATA_MEDAL # 用于存儲牌子數(shù)據(jù) data_medal_json = DATA_MEDAL # 請求頭 header1 = { 'content-type': 'text/plain; charset=utf-8', # 下方填入你的cookie喵 'cookie': "", 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.186.400 QQBrowser/11.3.5195.400' } # 計數(shù)用 num = 0 # 獲取主播牌子信息 傳入主播房間號 async def get_medal(roomid): global header1 API_URL = 'https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByUser?from=0¬_mock_enter_effect=1&room_id=' + str(roomid) async with aiohttp.ClientSession(headers=header1) as session: try: async with session.get(url=API_URL, headers=header1) as response: if response.status != 200: response.raise_for_status() ret = await response.json() except aiohttp.ClientError as e: print(e) # 睡眠個3s await asyncio.sleep(3) # 重試一次 async with session.get(url=API_URL, headers=header1) as response: if response.status != 200: response.raise_for_status() ret = await response.json() return ret async def main(): global data_medal_json, num # print(type(DATA)) # 遍歷本地vtb數(shù)據(jù) 第二個參數(shù)的起始值,跳過前n個數(shù)據(jù)(這個下標可以通過2.py獲取已加載到的下標) for data in islice(DATA, 4849, None): print(data) try: roomid = data["roomid"] except (KeyError, TypeError, IndexError) as e: print(e) continue if roomid == 0: continue # 睡眠個0.5s await asyncio.sleep(0.5) json1 = await get_medal(roomid) # print(json1) try: if json1["code"] != 0: print(json1) continue # 獲取牌子名 medal_name = str(json1["data"]["medal"]["up_medal"]["medal_name"]) # 拼接新的json串 temp_json = { medal_name: data } try: # 判斷是否已經(jīng)存在 if temp_json in DATA_MEDAL: print("已存在 " + medal_name + " 跳過") continue else: # 追加入json data_medal_json.append(temp_json) except (KeyError, TypeError, IndexError) as e: print(e) continue # 計數(shù)+1 num += 1 print("獲取牌子名:" + medal_name) # 每獲取10個結(jié)果 寫入一次數(shù)據(jù)文件 if num % 10 == 0 and num != 0: filename = 'data_medal.py' with open(filename, 'w', encoding="utf-8") as file_object: file_object.write("DATA_MEDAL = " + json.dumps(data_medal_json, ensure_ascii=False)) file_object.close() print("num=" + str(num) + ", 寫入" + filename) except (KeyError, TypeError, IndexError) as e: print(e) continue filename = 'data_medal.py' with open(filename, 'w', encoding="utf-8") as file_object: file_object.write("DATA_MEDAL = " + json.dumps(data_medal_json, ensure_ascii=False)) file_object.close() print("num=" + str(num) + ", 寫入" + filename) print("數(shù)據(jù)爬取完畢了,收工回家~") if __name__ == "__main__": asyncio.run(main())
到此這篇關(guān)于python實現(xiàn) 獲取b站主播直播間 粉絲牌信息 的文章就介紹到這了,更多相關(guān)python獲取b站主播直播間粉絲牌信息 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python合并Excel表(多sheet)的實現(xiàn)
這篇文章主要介紹了Python合并Excel表(多sheet)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04基于Python+QT的gui程序開發(fā)實現(xiàn)
這篇文章主要介紹了基于Python+QT的gui程序開發(fā)實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07Python3實現(xiàn)簡單可學習的手寫體識別(實例講解)
下面小編就為大家?guī)硪黄狿ython3實現(xiàn)簡單可學習的手寫體識別(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10