python教程

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

我的站长站 2022-08-21 人阅读

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

花了半小时写了微博自定义批量取消关注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()
相关推荐
  • Python代码
  • iqiyi视频解析Python代码

    最新吾爱大佬分享的一段iqiyi视频解析Python代码,转载分享给大家参考。代码说明m3u8下载部分:pip install m3u8download-hecoter使用需要nodejs项目链接:https://github.com/hecoter/videoParse/tree/main/iqiyipython代码import requestsimport reimpo...

    python教程 105 1年前
  • python开发一个桌面僵尸宠物代码

    python开发一个桌面行走的僵尸宠物代码,可切换僵尸皮肤,效果如下:python代码截图python代码如下# *_* coding : UTF-8 *_*# author : Leemamas# 开发时间 : 2021/5/28 0:48 import sysfrom PyQt5.QtGui import *from PyQt5.QtCore import *from PyQt...

    python教程 219 2年前
  • 获取免费的https代理Python代码

    前言大家用Python爬网页时候,爬快了被封IP,爬慢了,等的着急,这时候就需要https代理来切换IP了。分享一段获取免费的https代理Python代码,可以快速获取网络上免费的https代理。Python代码from multiprocessing.dummy import Lockimport reimport requestsi...

    python教程 92 2年前
最新更新