B站直播预约抽奖协议实现、扫描预约抽奖链接思路
通过预约直播间实现b站直播预约抽奖
b站登录协议请看点方蓝色字体
文章结尾附Python代码
一、抓包
1.1直播预约包
直播预约链接
POST 请求 https://api.bilibili.com/x/dynamic/feed/reserve/click?csrf=46378a87cb7283a133e9c32b9c09bee7
原始表单数据 {"reserve_id":4073369,"cur_btn_status":1,"dynamic_id_str":"976127207527153680","reserve_total":25439,"spmid":""}
{
"cur_btn_status": 1,
"dynamic_id_str": "976127207527153680",
"reserve_id": 4073369,
"reserve_total": 25439,
"spmid": ""
}
cur_btn_status——1:预约 2:取消预约
dynamic_id_str——直播预约的链接后面的id
reserve_id——直播间id
reserve_total——已预约人数
响应结果示例
1.2直播reserve_id、reserve_total获取包
只需要GET直播预约的链接即可在响应中找到
本文使用的是https://www.bilibili.com/opus/976127207527153680
响应中还有直播的时间,标题
reserve_total——reserve_total
rid——reserve_id
二、批量获取动态抽奖的思路
请看这个文章的第二部分有详细的思路教程
三、Python代码实现
2.1reserve_id、reserve_total获取
def get_bilibili_opus(cookie,articalid):L
url = 'https://www.bilibili.com/opus/{articalid}'
headers = {
'Host': 'www.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://www.bilibili.com/',
'Connection': 'keep-alive',
'Cookie': cookie # cookie
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text # 返回HTML内容
else:
return "获取失败"
2.2直播预约
def post_url(uid, url, data=None, headers=None, cookie_string=None):
try:
if cookie_string:
headers['Cookie'] = cookie_string
session = requests.Session()
response = session.post(url, data=data, headers=headers)
print(f'状态码: {response.status_code}')
if response.status_code == 200:
try:
if 'gzip' in response.headers.get('Content-Encoding', ''):
response_data = gzip.decompress(response.content).decode('utf-8')
else:
response_data = response.text
print("参与成功")
try:
json_data = json.loads(response_data)
print("响应数据:", json_data)
except json.JSONDecodeError:
pass
except Exception as e:
print("处理响应数据时出错:", e)
else:
print(f'请求失败,状态码:{response.status_code}')
print("响应内容:", response.text)
except Exception as e:
print("请求过程中发生错误:", e)
data = {
"card_type": "reserve",
"reserve_id": result['rid'],
"dynamic_id_str": article_id,
"cur_btn_status": 1,
"reserve_total": result['reserve_total']
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Content-Type': 'application/json',
'Referer': 'https://www.bilibili.com/',
'Origin': 'https://www.bilibili.com',
'Connection': 'keep-alive',
}
post_url(uid, f'https://api.bilibili.com//x/dynamic/feed/reserve/click?csrf={bili_jct}',
json.dumps(data), headers, cookie_string)