用脚本批量删除微博

  1. 首先,登录pc端的微博页面,并切换到自己微博列表的第一页(url 应该是 https://weibo.com/YOURNAME/profile...这样的形式 )
  2. 在 chrome 浏览器调试工具的 console 命令行( chrome如何打开控制台-百度经验
    )中,贴入以下代码:
const delay = 6180; // 间隔时间
const onlyForward = true; // 仅删转发

const ajwvr = 6;
const cfg = window.$CONFIG;
let pagebar = 0;

function getQuery() {
    const { protocol, host, pathname, search } = document.location;
    const query = search.substr(1).split('&').reduce((obj, item) => {
        const [key, value] = item.split('=');
        obj[key] = value;
        return obj;
    }, Object.create(null));
    return { ...document.location, query: { page: 1, profile_type: 1, ...query } };
}

function doDel() {
    const { protocol, host, pathname, search, query } = getQuery();
    var list = document.querySelectorAll('.WB_cardwrap.WB_feed_type');
    var nonselfList = onlyForward
        ? [].filter.call(list, ele => ele.querySelector('.WB_expand'))
        : list;
    if (!nonselfList.length) {
        // console.log(query, '没有', document.querySelector('.page.next'));
        return Promise.resolve(null);
    }
    var promises = nonselfList.map(ele => fetch(`${protocol}//${host}/aj/mblog/del?ajwvr=${ajwvr}`, {
      "headers": {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7",
        "cache-control": "no-cache",
        "content-type": "application/x-www-form-urlencoded",
        "pragma": "no-cache",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "x-requested-with": "XMLHttpRequest"
      },
      "referrer": `${protocol}//${host}${pathname}?profile_ftype=${query.profile_ftype}&is_all=1`,
      "referrerPolicy": "no-referrer-when-downgrade",
      "body": "mid=" + ele.getAttribute('mid'),
      "method": "POST",
      "mode": "cors",
      "credentials": "include"
    }));
    return Promise.all(promises);
}

function getMore() {
    const { protocol, host, pathname, search, query } = getQuery();
    const url = `${protocol}//${host}/p/aj/v6/mblog/mbloglist?ajwvr=${ajwvr}&domain=${cfg.domain}&is_search=0&visible=0&is_all=1&is_tag=0&profile_ftype=${query.profile_ftype}&page=${query.page}&pagebar=${pagebar++}&pl_name=Pl_Official_MyProfileFeed__20&id=${cfg.page_id}&script_uri=${pathname}&feed_type=0&pre_page=${query.page}&domain_op=${cfg.domain}&__rnd=${Date.now()}`;
    fetch(url)
        .then(res=>res.json())
        .then(json=>{
            const $lst = document.querySelector('[node-type=feed_list]');
            if (pagebar === 0) {
                $lst.innerHTML = json.data;
            } else {
                $lst.insertAdjacentHTML('beforeEnd', json.data);
            }
        })
        .then(checkPage);
}

function checkPage() {
    if (pagebar <= 1) {
        getMore();
        return;
    }
    doDel()
        .then((resArr) => {
            if (!resArr || !resArr.length || resArr.some(res => res.status >= 300)) {
                if (document.querySelector('.page.next')) {
                    console.log('next page', resArr, '崴');
                    setTimeout(() =>  document.querySelector('.page.next:last-of-type').click(), 500);
                    setTimeout(checkPage, delay * 2);
                } else {
                    getMore();
                }
                return;
            }
            pagebar = 0;
            console.log('next page', resArr);
            setTimeout(() => document.querySelector('.page.next').click(), 500);
            setTimeout(checkPage, delay);
        });
}

checkPage();
  1. 如果连续删除的已发微博数量较多,官方为防止操作过于频繁,每隔一段时间就会删除失败,这时只要等待即可,脚本会继续执行
  2. 该脚本还很原始,如果条数多,删除速度会很慢,请耐心使用
  3. 删到最后一页后,可能需要手动翻回首页重复若干次

你可能感兴趣的:(用脚本批量删除微博)