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用于存儲(chǔ)用戶結(jié)果數(shù)據(jù)
API
https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByUser?from=0¬_mock_enter_effect=1&room_id= 傳入主播的房間號(hào),解析json["data"]["medal"]["up_medal"]["medal_name"],即可。
使用
安裝相應(yīng)的第三方庫(kù)(aiohttp)后,python 1.py 即可。
核心源碼
1.py
import json
import asyncio
import aiohttp
import time
from itertools import islice
# data.py存儲(chǔ)著從vtbs.moe獲取的主播數(shù)據(jù)
from data import DATA
# data_medal.py用于存儲(chǔ)獲取的主播牌子信息
from data_medal import DATA_MEDAL
# 用于存儲(chǔ)牌子數(shù)據(jù)
data_medal_json = DATA_MEDAL
# 請(qǐng)求頭
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
# 獲取主播牌子信息 傳入主播房間號(hào)
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡(jiǎn)介Python設(shè)計(jì)模式中的代理模式與模板方法模式編程
這篇文章主要介紹了Python設(shè)計(jì)模式中的代理模式與模板方法模式編程,文中舉了兩個(gè)簡(jiǎn)單的代碼片段來說明,需要的朋友可以參考下2016-02-02
python中for語句簡(jiǎn)單遍歷數(shù)據(jù)的方法
這篇文章主要介紹了python中for語句簡(jiǎn)單遍歷數(shù)據(jù)的方法,以一個(gè)簡(jiǎn)單實(shí)例形式分析了Python中for語句遍歷數(shù)據(jù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05
Python?ModuleNotFoundError:?No?module?named?‘xxx‘可能的解決方
本文主要介紹了Python?ModuleNotFoundError:?No?module?named?‘xxx‘可能的解決方案大全,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧Chat?Gpt<BR>2023-07-07
python+selenium實(shí)現(xiàn)163郵箱自動(dòng)登陸的方法
本篇文章主要介紹了python+selenium實(shí)現(xiàn)163郵箱自動(dòng)登陸的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
opencv python 基于KNN的手寫體識(shí)別的實(shí)例
這篇文章主要介紹了opencv python 基于KNN的手寫體識(shí)別的實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
詳解matplotlib中pyplot和面向?qū)ο髢煞N繪圖模式之間的關(guān)系
這篇文章主要介紹了詳解matplotlib中pyplot和面向?qū)ο髢煞N繪圖模式之間的關(guān)系,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
對(duì)python中array.sum(axis=?)的用法介紹
今天小編就為大家分享一篇對(duì)python中array.sum(axis=?)的用法介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python中使用實(shí)現(xiàn)輸出哈沙德數(shù)的多種方法小結(jié)
哈沙德數(shù)(Harshad?Number),又稱Niven數(shù),是指一個(gè)自然數(shù),它可以被它的各位數(shù)字之和整除,本文將探討如何使用多種不同的方法來判斷一個(gè)數(shù)字是否是哈沙德數(shù),感興趣的可以了解下2024-01-01

