首页 > 编程语言 >【python】bilibili动态删除脚本

【python】bilibili动态删除脚本

时间:2024-05-12 14:42:00浏览次数:24  
标签:删除 python self dynamic response bilibili headers data id

import pprint
import requests
import json
import re
import os

# 最大删除条数
MAX_COUNT = 200
# 保存cookie的路径
COOKIE_FILE_PATH = r"./cookie.txt"

class BWebSite(object):

def __init__(self):
if not os.path.exists(COOKIE_FILE_PATH):
print("未检测到cookie,请手动输入:")
cookie = input()
if re.search('DedeUserID=(.+?);', cookie) and re.search('bili_jct=(.+?);', cookie):
with open(COOKIE_FILE_PATH, 'w', encoding='utf-8') as f:
f.write(cookie)
print("*" * 50)
print("输入成功".center(50))
print("*" * 50)
else:
print('cookie输入错误')
exit()
else:
with open(COOKIE_FILE_PATH, 'r', encoding='utf-8') as f:
cookie = f.read()
if not cookie:
print("cookie为空,请重新填入cookie")
os.remove(COOKIE_FILE_PATH)
exit()
self.uid = re.search('DedeUserID=(.+?);', cookie)
self.token = re.search('bili_jct=(.+?);', cookie)
if not (self.uid and self.token):
print("cookie错误,请重新填入")
os.remove(COOKIE_FILE_PATH)
exit()
self.uid = self.uid.group(1)
self.token = self.token.group(1)
self.headers = {
'authority': 'api.vc.bilibili.com',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
'accept': 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36',
'sec-ch-ua-platform': '"Windows"',
'origin': 'https://space.bilibili.com',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'accept-language': 'zh-CN,zh;q=0.9',
'cookie': f'{cookie}',
}

# 获取动态信息
@property
def dynamic(self):
dynamic_id = 0
params = (
('visitor_uid', f'{self.uid}'),
('host_uid', f'{self.uid}'),
('offset_dynamic_id', f'{dynamic_id}'),
('need_top', '1'),
('platform', 'web'),
)
list_cards = []
response = requests.get('https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history',
headers=self.headers,
params=params)
data = json.loads(response.text)['data']
while data.get('cards'):
cards = data['cards']
dynamic_id = data['next_offset']
for card in cards:
list_cards.append(card)
if len(list_cards) >= MAX_COUNT:
return list_cards
params = (
('visitor_uid', f'{self.uid}'),
('host_uid', f'{self.uid}'),
('offset_dynamic_id', f'{dynamic_id}'),
('need_top', '1'),
('platform', 'web'),
)
response = requests.get('https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history',
headers=self.headers,
params=params)
data = json.loads(response.text)['data']
return list_cards

# 删除动态信息
def delete_dynamic(self):
list_dynamic_id = []
for dynamic in self.dynamic:
list_dynamic_id.append(dynamic['desc']['dynamic_id'])
for dynamic_id in list_dynamic_id:
data = {
'dynamic_id': f'{dynamic_id}',
'csrf_token': f'{self.token}',
'csrf': f'{self.token}'
}
response = requests.post('https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/rm_dynamic',
headers=self.headers,
data=data)
code = json.loads(response.text)['code']
if code == 0:
print(f"{dynamic_id}删除成功")

# 获取所有回复
@property
def comments(self):
params = (
('platform', 'web'),
('build', '0'),
('mobi_app', 'web'),
)
response = requests.get('https://api.bilibili.com/x/msgfeed/reply', headers=self.headers, params=params)
ls = []
data = json.loads(response.text)['data']
while not data['cursor']['is_end']:
ls.append(data)
if len(ls) > MAX_COUNT:
return ls
cursor = data['cursor']
comment_id = cursor['id']
time = cursor['time']
params = (
('id', f'{comment_id}'),
('reply_time', f'{time}'),
('platform', 'web'),
('build', '0'),
('mobi_app', 'web'),
)
response = requests.get('https://api.bilibili.com/x/msgfeed/reply', headers=self.headers, params=params)
data = json.loads(response.text)['data']
return ls

# 删除回复
def delete_comment(self):
for comment in self.comments:
for item in comment['items']:
subject_id = item['item']['subject_id']
target_id = item['item']['target_id']
title = item['item']['title']
data = {
'oid': f'{subject_id}',
'type': '1',
'jsonp': 'jsonp',
'rpid': f'{target_id}',
'csrf': f'{self.token}'
}
response = requests.post('https://api.bilibili.com/x/v2/reply/del', headers=self.headers, data=data)
# data = json.loads(response.text)
# print(data)
code = json.loads(response.text)['code']
if code == 0:
print(f"{title}-----------下面的回复已删除")

# 获取所有收到赞的回复
@property
def likes(self):
params = (
('platform', 'web'),
('build', '0'),
('mobi_app', 'web'),
)
response = requests.get('https://api.bilibili.com/x/msgfeed/like', headers=self.headers, params=params)
ls = []
data = json.loads(response.text)['data']
total = json.loads(response.text)['data']['total']
while not total['cursor']['is_end']:
ls.append(data)
if len(ls) > MAX_COUNT:
return ls
cursor = total['cursor']
comment_id = cursor['id']
time = cursor['time']
params = (
('id', f'{comment_id}'),
('like_time', f'{time}'),
('platform', 'web'),
('build', '0'),
('mobi_app', 'web'),
)
response = requests.get('https://api.bilibili.com/x/msgfeed/like', headers=self.headers, params=params)
data = json.loads(response.text)['data']
total = json.loads(response.text)['data']['total']
return ls

def delete_like(self):
for like in self.likes:
for item in like['total']['items']:
native_uri = item['item']['native_uri']
item_id = item['item']['item_id']
title = item['item']['title']
rpid = item_id
print(native_uri, title)
oid = re.search("//video/(\d+)", native_uri)
if not oid:
oid = re.search('//following/detail/(\d+)', native_uri)
if not oid:
oid = re.search('//album/(\d+)', native_uri)
if not oid:
continue
oid = oid.group(1)
print(oid, rpid)
data = {
'oid': f'{oid}',
'type': '1',
'jsonp': 'jsonp',
'rpid': f'{rpid}',
'csrf': f'{self.token}'
}
response = requests.post('https://api.bilibili.com/x/v2/reply/del', headers=self.headers, data=data)
# data = json.loads(response.text)
# print(data)
code = json.loads(response.text)['code']
if code == 0:
print(f"{title}-----------下面的回复已删除")

# 获取所有关注
@property
def follows(self):
vmid = f'{self.uid}'
pn = 1
list_follows = []
h = self.headers
h.update(
{
'referer': f'https://space.bilibili.com/{self.uid}/fans/follow?tagid=-10',
}
)
while True:
params = (
('vmid', f'{vmid}'),
('pn', f'{pn}'),
('ps', '20'),
('order', 'desc'),
('order_type', 'attention'),
('jsonp', 'jsonp'),
('callback', '__jp7'),
)
response = requests.get('https://api.bilibili.com/x/relation/followings', headers=h,
params=params)
data = json.loads(response.text[response.text.index('(') + 1:response.text.rindex(')')])['data']
list_data = data['list']
total = data['total']
for item in list_data:
list_follows.append(item)
if total > MAX_COUNT and len(list_follows) == MAX_COUNT:
break
if len(list_follows) == total:
break
pn += 1
return list_follows

# 删除所有
def delete_follows(self):
follows = self.follows
for follow in follows:
mid = follow['mid']
uname = follow['uname']
data = {
'fid': f'{mid}',
'act': '2',
're_src': '11',
'spmid': '333.999.0.0',
'extend_content': f'{{"entity":"user","entity_id":{mid}}}',
'jsonp': 'jsonp',
'csrf': f'{self.token}'
}
response = requests.post('https://api.bilibili.com/x/relation/modify', headers=self.headers, data=data)
code = json.loads(response.text)['code']
if code == 0:
print(f'{uname}----------取消关注成功')

web = BWebSite()
# web.delete_comment()
# web.delete_like()
web.delete_dynamic()
# pprint.pprint(web.follows)
# web.delete_follows()

#获取cookie:https://www.bilibili.com/video/BV17Q4y177ip/?spm_id_from=333.337.search-card.all.click&vd_source=3b08e97e50222fa2ec22737f6dcb2202

标签:删除,python,self,dynamic,response,bilibili,headers,data,id
From: https://www.cnblogs.com/wk2522466153/p/18187805

相关文章

  • 代码随想录算法训练营第第二天 | 24. 两两交换链表中的节点 、19.删除链表的倒数第N
    两两交换链表中的节点用虚拟头结点,这样会方便很多。本题链表操作就比较复杂了,建议大家先看视频,视频里我讲解了注意事项,为什么需要temp保存临时节点。题目链接/文章讲解/视频讲解:https://programmercarl.com/0024.两两交换链表中的节点.html/***Definitionforsingly-li......
  • python获取问津
    importosimportrequestsimportjsonimportrefrombs4importBeautifulSoupfromqiniuimportAuth,put_fileclassBilibiliVideoAudio:def__init__(self,bid,download_path='download'):self.bid=bidself.headers={&qu......
  • 【攻防技术系列】-- Python沙箱逃逸
    Python是一种强大而灵活的编程语言,但在某些情况下,可能需要运行不受信任的代码,同时又希望限制它的行为,以防止对系统的不良影响。这时,Python沙箱就成为一种有用的工具,它可以帮助你在安全的环境中运行不受信任的代码。本文将探讨Python沙箱的概念、常见的沙箱技术以及如何避免沙......
  • Playwright安装与Python集成:探索跨浏览器测试的奇妙世界
    前言web自动化测试是我们工作中的重要一环,随着Web应用程序和网站的复杂性不断增加,跨浏览器测试变得越来越必要。而Playwright作为一种新兴的跨浏览器测试工具,为开发人员提供了一种轻松愉快的方式来进行自动化测试。本文将带您深入探索Playwright的安装过程,并结合Python语言,揭示......
  • 神器!使用Python 轻松识别验证码
    前言在我们进行自动化测试的过程中,免不了要在登录时遇到验证码,很多时候我们都是只能找开发要万能验证码或者暂时关闭验证码这个功能,但是有时候我们必须要验证码是否能够正常生成,所以在这个时候,我们需要做的就是输入验证码,但是验证码这个东西是随机生成的,不是每一次都一样,所以我......
  • Python 潮流周刊#50:我最喜欢的 Python 3.13 新特性!
    本周刊由Python猫出品,精心筛选国内外的250+信息源,为你挑选最值得分享的文章、教程、开源项目、软件工具、播客和视频、热门话题等内容。愿景:帮助所有读者精进Python技术,并增长职业和副业的收入。本期分享了12篇文章,11个开源项目,2则音视频,赠书5本《黑客与画家(10万册纪......
  • 数据分享|python分类预测职员离职:逻辑回归、梯度提升、随机森林、XGB、CatBoost、LGB
    全文链接:https://tecdat.cn/?p=34434原文出处:拓端数据部落公众号分析师:ShilinChen离职率是企业保留人才能力的体现。分析预测职员是否有离职趋向有利于企业的人才管理,提升组织职员的心理健康,从而更有利于企业未来的发展。解决方案任务/目标采用分类这一方法构建6种模型对职......
  • 使用Python根据网页生成RSS
    pipinstallrequestsbeautifulsoup4lxmlimportrequestsfrombs4importBeautifulSoupimportxml.etree.ElementTreeasETdeffetch_news_from_url(url):#1.网页抓取response=requests.get(url)response.raise_for_status()......
  • python 换源命令
    #换阿里云的源$pipconfigsetglobal.index-urlhttps://mirrors.aliyun.com/pypi/simple$pipconfigsetinstall.trusted-hostmirrors.aliyun.com#换清华大学的源$pipconfigsetglobal.index-urlhttps://pypi.tuna.tsinghua.edu.cn/simple$pipconfigsetinstall......
  • 牛客小白月赛93(python)
    A生不逢71defcheck(num):2return'7'instr(num)ornum%7==034defsolve():5n,a,k=LII()6d=a+17foriinrange(k):8ifcheck(d):9print('p',end='')10els......