菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

使用Python编写微博批量取消关注的代码

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 Python代码

要实现微博自定义批量取消关注,可以使用Python的第三方库`weibospider`。首先需要安装这个库,然后编写代码实现登录、获取粉丝列表和取消关注的功能。以下是一个简单的示例: 1. 安装weibospider库:`pip install weibospider` 2. 编写代码: ```python from weibospider import WeiboSpider # 替换为你的微博用户名和密码 username = 'your_username' password = 'your_password' # 初始化WeiboSpider对象 ws = WeiboSpider(username, password) # 获取粉丝列表 fans = ws.get('friendships/friends') # 取消关注的粉丝ID列表 unfollow_ids = [fan['id'] for fan in fans] # 取消关注 ws.post('friendships/destroy_

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

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

importrequestsfromjsonpathimportjsonpathimportosdefget_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)ifpage*50<total_number:page=page+1iftotal_number<50:returnfollow_list#如果关注的人低于50个一般是只有一页直接返回关注ID列表foriinrange(1,page):ifi+1*50>total_number:breakurl=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+resultreturnfollow_listdefget_white_list():ifnotos.path.exists('不取消关注列表.txt'):withopen('不取消关注列表.txt','w')asf:f.write('请将不取消关注列表通过ID换行的方式写入,例如:7475835448n3660350872')f.close()returnNonereturnopen('不取消关注列表.txt','r',encoding='utf-8').read().split('n')defdestroyBatch(headers,destroylist):foriindestroylist: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)forxinget_first_followContent(headers)]white_lists=get_white_list()#获取白名单ifwhite_listsisnotNone:forjinwhite_lists:ifjnotinresult:continueresult.remove(j)destroyBatch(headers,result)#get_first_followContent()

相关文章

    无相关信息
评论
建站知识
建站知识
使用技巧
调试安装
运营推广