前言 用於實現通過牌子逆向查主播信息這個功能。 插件基於Nonebot2開發,鏈接:https://github.com/Ikaros-521/nonebot_plugin_searchBiliInfo 工程下載 github:https://github.com/Ikaros-521/get_bi ...
前言
用於實現通過牌子逆向查主播信息這個功能。
插件基於Nonebot2開發,鏈接: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
目錄結構
data.py數據源自vtbs.moe
1.py用於獲取數據
2.py用於中斷時候的下標檢索
data_medal.py用於存儲用戶結果數據
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"]
,即可。
使用
安裝相應的第三方庫(aiohttp)後,python 1.py
即可。
核心源碼
1.py
import json
import asyncio
import aiohttp
import time
from itertools import islice
# data.py存儲著從vtbs.moe獲取的主播數據
from data import DATA
# data_medal.py用於存儲獲取的主播牌子信息
from data_medal import DATA_MEDAL
# 用於存儲牌子數據
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'
}
# 計數用
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數據 第二個參數的起始值,跳過前n個數據(這個下標可以通過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:
# 判斷是否已經存在
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
# 計數+1
num += 1
print("獲取牌子名:" + medal_name)
# 每獲取10個結果 寫入一次數據文件
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("數據爬取完畢了,收工回家~")
if __name__ == "__main__":
asyncio.run(main())