b站协议实现点赞收藏投币保姆级教学
b站登录协议请看点方蓝色字体
文章结尾附Python代码
目录
一、抓包
1.1点赞
1.1.1点赞包
POST请求 https://api.bilibili.com/x/web-interface/archive/like
原始表单数据aid=113173461999500&like=1&eab_x=1&ramval=7&source=web_normal&ga=1&csrf=46378a87cb7283a133e9c32b9c09bee7
{
"aid": "113173461999500",
"like": "1",
"eab_x": "1",
"ramval": "7",
"source": "web_normal",
"ga": "1",
"csrf": "46378a87cb7283a133e9c32b9c09bee7"
}
aid——视频id
like——1:点赞 2:取消点赞
csrf——登录Cookie可找到
响应结果
1.1.2取消点赞
{
"aid": "113173461999500",
"like": "2",
"eab_x": "2",
"ramval": "440",
"source": "web_normal",
"ga": "1",
"csrf": "46378a87cb7283a133e9c32b9c09bee7"
}
取消点赞的POST地址跟点赞一样,表单数据也基本一样
只需要把点赞的数据包里的lkie的值变为2即可
1.1.3 aid 获取
aid只需要GET请求视频的链接即可在响应中获取
1.2收藏
1.3.1收藏包
POST 请求 https://api.bilibili.com/x/v3/fav/resource/deal
原始表单数据rid=113173461999500&type=2&add_media_ids=773769484&del_media_ids=&platform=web&eab_x=2&ramval=440&ga=1&gaia_source=web_normal&csrf=46378a87cb7283a133e9c32b9c09bee7
{
"rid": "113173461999500",
"type": "2",
"add_media_ids": "773769484",
"del_media_ids": "",
"platform": "web",
"eab_x": "2",
"ramval": "440",
"ga": "1",
"gaia_source": "web_normal",
"csrf": "46378a87cb7283a133e9c32b9c09bee7"
}
rid——视频id 就是点赞的aid
add_media_ids——收藏夹id
响应结果
1.3.2 收藏夹id获取
rid——视频id 就是点赞的aid
up_mid——自己的b站uid
响应结果
可以看到响应中的773769484就是收藏包里的 add_media_ids
1.3投币
投币两个 POST请求 https://api.bilibili.com/x/web-interface/coin/add
原始表单aid=113173461999500&multiply=2&select_like=0&cross_domain=true&eab_x=2&ramval=440&source=web_normal&ga=1&csrf=46378a87cb7283a133e9c32b9c09bee7
{
"aid": "113173461999500",
"multiply": "2",
"select_like": "0",
"cross_domain": "true",
"eab_x": "2",
"ramval": "440",
"source": "web_normal",
"ga": "1",
"csrf": "46378a87cb7283a133e9c32b9c09bee7"
}
aid——视频id
multiply——投币数量 1或者2
响应结果
三、Python代码示例
3.1收藏夹id获取
def get_id(cookie, rid, up_mid):
url = 'https://api.bilibili.com/x/v3/fav/folder/created/list-all'
params = {
'type': 2,
'rid': rid,
'up_mid': up_mid
}
headers = {
'Host': 'api.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.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',
'Referer': 'https://www.bilibili.com/video/BV1XctrehENu/?spm_id_from=333.1007.tianma.3-4-10.click&vd_source=c4a057c9ccc572303e9ab37c6732a578',
'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://www.bilibili.com',
'Connection': 'keep-alive',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'Priority': 'u=0',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'TE': 'trailers',
'Cookie': cookie # 将cookie放入请求头
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
return response.json() # 返回JSON格式的数据
else:
return '收藏失败'
3.2点赞
import requests
def post_bilibili_like(cookie, aid, csrf_token):
url = 'https://api.bilibili.com/x/web-interface/archive/like'
# 表单数据
data = {
'aid': aid,
'like': 1,
'eab_x': 1,
'ramval': 7,
'source': 'web_normal',
'ga': 1,
'csrf': csrf_token
}
headers = {
'Host': 'api.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.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',
'Referer': f'https://www.bilibili.com/video/BV{aid}',
'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://www.bilibili.com',
'Connection': 'keep-alive',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Cookie': cookie #cookie
}
# 发送POST请求
response = requests.post(url, headers=headers, data=data)
# 返回请求结果
if response.status_code == 200:
return response.json() # 返回JSON格式的数据
else:
return '点赞失败'