欢迎来到星网!免费注册并寻找你的资源,就在这里!

现在注册

Python 小项目

欢迎!小凯想睡觉 邀请您加入我们的社区。请 点击这里注册

Hifive

会员
注册
2022-11-06
消息
14
0
声望
3
金币
602
【python】一键取消微博关注

可以设置白名单列表(具体看代码
请自行复制请求头(包含cookie,划重点)

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 换行的方式写入,例如:7475835448\n3660350872')
            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()
 

yiny

站长
管理成员
站长
星之彼岸系开发者
注册
2022-05-02
消息
240
155
声望
44
等级
3
徽章
15
金币
54,438
  • 二刺猿徽章
  • 链接
  • 二刺猿徽章
  • StarLight
是有用的(确信)
 
顶部 底部