python實(shí)現(xiàn) 獲取b站主播直播間 粉絲牌信息的方法
前言
用于實(shí)現(xiàn)通過牌子逆向查主播信息這個(gè)功能。
插件基于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用于中斷時(shí)候的下標(biāo)檢索
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' } # 計(jì)數(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) # 睡眠個(gè)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ù) 第二個(gè)參數(shù)的起始值,跳過前n個(gè)數(shù)據(jù)(這個(gè)下標(biāo)可以通過2.py獲取已加載到的下標(biāo)) 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 # 睡眠個(gè)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 # 計(jì)數(shù)+1 num += 1 print("獲取牌子名:" + medal_name) # 每獲取10個(gè)結(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實(shí)現(xiàn) 獲取b站主播直播間 粉絲牌信息 的文章就介紹到這了,更多相關(guān)python獲取b站主播直播間粉絲牌信息 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python合并Excel表(多sheet)的實(shí)現(xiàn)
這篇文章主要介紹了Python合并Excel表(多sheet)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04基于Python+QT的gui程序開發(fā)實(shí)現(xiàn)
這篇文章主要介紹了基于Python+QT的gui程序開發(fā)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07基于Python實(shí)現(xiàn)敲擊木魚積累功德效果
最近大家都很流行用手機(jī)敲擊電子木魚積累功德,這在很多短視頻中也常常見到。本文將用Python語言實(shí)現(xiàn)這一效果,感興趣的小伙伴開業(yè)了解一下2022-11-11Python?查找算法之二分查找線性查找與哈希查找實(shí)例探究
這篇文章主要為大家介紹了Python查找算法探究之二分查找、線性查找與哈希查找的實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Selenium啟動(dòng)Chrome時(shí)配置選項(xiàng)詳解
這篇文章主要介紹了Selenium啟動(dòng)Chrome時(shí)配置選項(xiàng)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Python3實(shí)現(xiàn)簡單可學(xué)習(xí)的手寫體識別(實(shí)例講解)
下面小編就為大家?guī)硪黄狿ython3實(shí)現(xiàn)簡單可學(xué)習(xí)的手寫體識別(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10