自動刪除QQ空間指定好友的留言

 

你是否也有過,想刪除QQ空間裏某個人的對自己發表的說說的全部評論,但又因說說太多,手動查找再刪除太過麻煩?OK,我也有這個需求,成品分享給你。

 

如果想自己琢磨呢,源碼在文末;如果想直接使用呢,exe已上傳,不用積分即可下載。

 


 

一、使用本程序,需要提供4個內容:你的QQ號、對方的名稱、g_tk和cookie。

1、你的QQ號

就是你的QQ號,複製到conf.json文件

 

2、對方的名稱

要刪除的對方的備註或暱稱(ta在你空間所顯示的名稱),一般如果你設置了備註,就是備註名;沒設置備註,就是他的網名。複製到conf.json文件。可以多個好友,以英文逗號分隔。

 

3、g_tk

a. 首先手動登陸你的QQ空間

 

b. 點進“我的主頁”

 

c. 按F12,選中network(有的瀏覽器顯示是“網絡”)

 

d. 刷新一下瀏覽器,點擊有html的一項

 

e. 最下面就有g_tk了,複製到conf.json文件

界面先不要關

 

4、cookie

g_tk獲取完,上面就有cookie,複製到conf.json文件

 

二、運行程序

 

三、源碼

import time
import requests
import json

with open('conf.json', 'r', encoding='utf-8') as f:
    content = f.read()
    msg = json.loads(content)
    print(msg)

# 你的QQ號
QQ = msg['QQ']
# 瀏覽器打開QQ空間,按F12,找到g_tk
g_tk = msg['g_tk']
# 要刪除的對方的備註或暱稱(ta在你空間所顯示的名稱)
targetname = msg['name'].split(',')
# 瀏覽器打開QQ空間,按F12,找到cookie
cookie = msg['cookie']

print('@'*60)
print('>> 你的QQ:', QQ)
print('>> 對方名稱:', targetname)
print('@'*60)
print('>> 開始運行')
print()

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36',
    'cookie': cookie
}

def getcomment():
    delcnt = 0
    delmsg = []
    totalcomment = 20
    pos = 0
    while pos < totalcomment:
        print('#' * 60)
        print(">> 當前位置:", pos)
        url = 'https://user.qzone.qq.com/proxy/domain/taotao.qq.com/cgi-bin/emotion_cgi_msglist_v6?uin={0}&inCharset=utf-8&outCharset=utf-8&hostUin={0}&notice=0&sort=0&pos={1}&num=20&cgi_host=https%3A%2F%2Fuser.qzone.qq.com%2Fproxy%2Fdomain%2Ftaotao.qq.com%2Fcgi-bin%2Femotion_cgi_msglist_v6&code_version=1&format=json&need_private_comment=1&g_tk={2}'.format(QQ, pos, g_tk)
        pos += 20
        html = requests.get(url, headers=headers).json()
        totalcomment = int(html['total'])
        print('>> 總共條數:', totalcomment)
        msglist = html['msglist']

        for item in msglist:
            try:
                commentlist = item['commentlist']
                conlist = item['conlist']
                if conlist:
                    conlist = conlist[0]['con']
                    print(">> 發佈的說說:", conlist)
                else:
                    print(">> 發佈僅爲圖片")
                topicId = QQ+'_' + item['tid']
                print(">> topicId:", topicId)
                for i in commentlist:
                    content = i['content']
                    name = i['name']
                    createTime = i['createTime']
                    commentId = commentlist.index(i)+1
                    print(">> 評論{}: ({}){}\t{}".format(commentId, createTime, name, content))
                    if name in targetname:
                        print(">> 刪除本條留言")
                        delcomment(topicId, commentId)
                        delcnt += 1
                        delmsg.append(content)
                print()
            except:
                pass
        time.sleep(1)
    print('*' * 60)
    print('共刪除條數:', delcnt)
    print('共刪除內容:', delmsg)
    print('*' * 60)


def delcomment(topicId, commentId):
    url = 'https://user.qzone.qq.com/proxy/domain/taotao.qzone.qq.com/cgi-bin/emotion_cgi_delcomment_ugc?g_tk={0}'.format(g_tk)
    data={
        'uin': QQ,
        'hostUin': QQ,
        'topicId': topicId,
        'commentId': commentId,
        'inCharset':'',
        'outCharset':'',
        'ref':'',
        'hostuin': QQ,
        'code_version': '1',
        'format': 'fs',
        'qzreferrer': 'https://user.qzone.qq.com/proxy/domain/qzs.qq.com/qzone/app/mood_v6/html/index.html#mood&g_iframeUser=1&g_iframedescend=1&uin={0}&pfid=2&qz_ver=8&appcanvas=0&qz_style=31&params=&entertime=1588985689146&canvastype=&cdn_use_https=1'.format(QQ)
    }
    html = requests.post(url, headers=headers, data=data)
    time.sleep(1)
    html = requests.post(url, headers=headers, data=data)
    if '對不起,原文已經被刪除,無法查看' in html.text:
        print('原文已經被刪除')

try:
    getcomment()
except Exception as e:
    print(e)
input(">> 任意鍵退出...")

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章