哔哩哔哩批量获取关注用户&批量关注用户脚本
批量获取关注用户uid脚本
import requests
uid = 386220197 # 用户UID
page_size = 50
# 创建一个session对象
session = requests.Session()
# 禁用系统代理
session.trust_env = False
def get_following_list():
headers = {
'Host': 'api.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.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',
'Referer': f'https://space.bilibili.com/{uid}/fans/follow?spm_id_from=444.41.my-info.follow.click',
'Origin': 'https://space.bilibili.com',
'Connection': 'keep-alive',
'Cookie': "你的Cookie",
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'TE': 'trailers'
}
following_ids = []
for page in range(1, 8):
params = {
'vmid': uid,
'pn': page,
'ps': page_size,
'order': 'desc',
'order_type': 'attention'
}
response = session.get('https://api.bilibili.com/x/relation/followings', headers=headers, params=params)
data = response.json()
if data['code'] == 0:
following_list = data['data']['list']
for following in following_list:
following_id = following['mid']
following_ids.append(following_id)
return following_ids
following_ids = get_following_list()
print(f'关注用户的ID列表: {following_ids}')
print(f'全部关注: {len(following_ids)}')
获取自己用户uid:https://space.bilibili.com
F12网络获取Cookie:
批量关注用户脚本
import requests
uid_list = [] # 用户UID列表,例如:uid_list = [386220197, 386220197]
csrf_token = '你的csrf_token' # 在浏览器中登录哔哩哔哩账号后,在开发者工具中获取csrf_token的值并填入此处
cookie = "你的Cookie" # 在浏览器中登录哔哩哔哩账号后,在开发者工具中获取cookie的值并填入此处
# 创建一个session对象
session = requests.Session()
# 禁用系统代理
session.trust_env = False
def batch_follow():
headers = {
'Host': 'api.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.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',
'Referer': 'https://space.bilibili.com/114866',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': '429',
'Origin': 'https://space.bilibili.com',
'Connection': 'keep-alive',
'Cookie': cookie,
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'TE': 'trailers'
}
for uid in uid_list:
data = {
'fid': uid,
'act': 1, #1是关注,2是取关,5是拉黑,6是取消拉黑
're_src': 11,
'jsonp': 'jsonp',
'csrf': csrf_token
}
response = session.post('https://api.bilibili.com/x/relation/modify', headers=headers, data=data)
print(response.text)
print(f'正在关注用户ID为: {uid}')
batch_follow()
打开F12点击网络任意关注获取到csrf_token