根据@cdsgg 大佬的代码进行修改
原贴:微博批量取关 可保留不取消关注白名单 - 『编程语言区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|[url]www.52pojie.cn[/url]
header 请求头获取方式 需要到 微博取消关注一个 把请求头复制下来
可以通过F12进行获取,点取消关注以后 找到 destory ,里面就是我们要的header请求头的信息
python版本:3.7.9
[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)
follow_list = jsonpath(r.json(), "$.data.follows.users[*].id") or []
if follow_list:
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
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'
req = requests.get(url, headers=headers).json()
result = jsonpath(req, "$.data.follows.users[*].id") or []
follow_list.extend(result) # 修改此行,使用 extend 函数拼接列表
else:
print("未获取到关注 ID 列表")
return follow_list
def get_white_list():
"""
获取需要保留关注的白名单
:return:
"""
return open('不取消关注列表.txt', 'r', encoding='gbk').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 = {
'cookie': '**********************************',
'origin': 'https://weibo.com',
'referer': 'https://weibo.com/u/page/follow/******/followGroup',
'server-version': 'v2023.04.27.3',
'traceparent': '00-************-00',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
'x-requested-with': '****',
'x-xsrf-token': '***********',
}
# 请求头请自行复制
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)
|