博客园批量删除标签

很久不用博客园了,最近改了改主题,发现一堆垃圾标签,但是博客园官方没有提供批量删除的地方,所以简单写了一个脚本,Python写的代码:

import requests
import json
import time

postids = []
tags = ['编程', '从入门到精通', 'Linux', '新手', '电脑', 'Linux入门', '编程', 'java', '计算机', '方法','菜鸟', 'J2SE', 'acm', 'Linux发展史', 'GUI', 'mui', 'c++细节', 'C++', '磁盘管理', '动态规划', 'wifi', 'round', 'python3', 'PHP', 'ORACLE', 'nyoj', 'MYSQL', 'Java Web', 'Firefox Browser', 'cnsimo', 'Chrome', '异常处理', '网络', '算法', '四舍五入']
postsapibase = 'https://i-beta.cnblogs.com/api/posts/'
postslistapi = 'https://i-beta.cnblogs.com/api/posts/list'
cookies = ''
headers = {"accept": "application/json, text/plain, */*", "accept-encoding": "gzip, deflate, br", "content-type": "application/json", "x-blog-id": ""}

def parseCookies():
    r = {}
    cl = cookies.split(";")
    for c in cl:
        m = c.split('=', 1)
        r[m[0].strip()] = m[1]
    return r

def getPostInfo(id):
    r = requests.get(postsapibase+id, cookies=cookies)
    return r.json()

def delTags(id):
    postinfo = getPostInfo(id)['blogPost']
    ts = postinfo['tags']
    newts = []
    for t in ts:
        if not t in tags:
            newts.append(t)
    postinfo['tags'] = newts
    headers["x-blog-id"] = str(postinfo["blogId"])
    r = requests.post(postsapibase, headers=headers, data=json.dumps(postinfo), cookies=cookies)
    return r.status_code
    pass

def getAllPosts():
    posts = []
    count = 0
    params = {'p': 1, 't': 1, 'cfg': 0}
    while True:
        r = requests.get(postslistapi, cookies=cookies, params=params)
        posts.extend(r.json()['postList'])
        count += len(r.json()['postList'])
        if count >= r.json()['postsCount']:
            return posts
        params['p'] += 1

if __name__ == '__main__':
    cookies = parseCookies()
    postsList = getAllPosts()
    for p in postsList:
        postids.append(str(p['id']))
    # print(postids)
    for id in postids:
        if delTags(id) != 200:
            print(id + ': 失败!')
        else:
            print(id + ': 成功!')
        time.sleep(1)

cookies中填入自己的cookies,运行后直接删除,没有确认,所以谨慎使用。

上一篇:对queryset的理解


下一篇:wordpress文章数量统计函数wp_count_posts()