自定义批量取消关注Python代码

用过微博都知道,微博自带的批量取消关注功能非常不好用,有跟没有一样,还是要一个个点,麻烦的一比。

花了半小时写了微博自定义批量取消关注Python代码,用作批量取消关注,如果有部分,不想取消掉,还可以在白名单里设置。

import requests
from jsonpath import jsonpath
import os


def get_first_followContent(headers):
"""
这个方法里面 会先获取 总共的关注数量 一页大概50个 如果第二页也超过50个 则进行下一页 以此类推
:return:
"""

r = requests.get('https://weibo.com/ajax/profile/followContent?sortType=all?sortType=all', headers=headers)

# print(r.json())
follow_list = jsonpath(r.json(), "$.data.follows.users.id")
print(len(follow_list))
total_number = int(jsonpath(r.json(), "$.data.follows.total_number")[0]) # 总共关注的数量
page = int(total_number / 50)

if page * 50 < total_number:
page = page + 1
if total_number < 50:
return follow_list # 如果关注的人低于50个 一般是只有一页直接返回关注ID列表

for i in range(1, page):
if i + 1 * 50 > total_number:
break

url = f'https://weibo.com/ajax/profile/followContent?page={i + 1}&next_cursor=50'
print(url)

req = requests.get(url, headers=headers).json()

result = jsonpath(req, "$.data.follows.users.id")

follow_list = follow_list + result

return follow_list


def get_white_list():
if not os.path.exists('不取消关注列表.txt'):
with open('不取消关注列表.txt', 'w') as f:
f.write('请将不取消关注列表 通过ID 换行的方式写入,例如:7475835448n3660350872')
f.close()
return None
return open('不取消关注列表.txt', 'r', encoding='utf-8').read().split('n')


def destroyBatch(headers, destroylist):
for i in destroylist:
result = requests.post('https://weibo.com/ajax/friendships/destory', json={"uid": "%s" % i}, headers=headers)

print(result.json())


if __name__ == '__main__':
headers = {

}
# 请求头请自行复制
result = [str(x) for x in get_first_followContent(headers)]

white_lists = get_white_list() # 获取白名单

if white_lists is not None:
for j in white_lists:
if j not in result:
continue
result.remove(j)

destroyBatch(headers, result)
# get_first_followContent()
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI虎哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值