代码拉取完成,页面将自动刷新
import json
import os
import requests
from datetime import datetime, timedelta
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
class CsdnCommentsManager:
HISTORY_FILE = 'history.json'
@staticmethod
def load_history():
if os.path.exists(CsdnCommentsManager.HISTORY_FILE):
with open(CsdnCommentsManager.HISTORY_FILE, 'r') as file:
return json.load(file)
return []
@staticmethod
def save_history(history):
with open(CsdnCommentsManager.HISTORY_FILE, 'w') as file:
json.dump(history, file)
@staticmethod
def update_history(history, username):
if username in history:
history.remove(username)
history.insert(0, username)
CsdnCommentsManager.save_history(history)
@staticmethod
def get_comments_count(username, date):
api_url = f"https://blog.csdn.net/community/home-api/v1/comment-list?page=1&size=220&type=out&noMore=false&username={username}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0"
}
response = requests.get(api_url, headers=headers)
if response.status_code != 200:
print(f"请求出错: {response.status_code}")
return -1
try:
data = response.json()
comments = data['data']['list']
target_date_comments = [comment for comment in comments if comment['createTime'].split()[0] == date]
return len(target_date_comments)
except Exception as e:
print(f"发生错误: {e}")
return -1
def fetch_comments(combobox):
username = combobox.get()
today_date = datetime.now().strftime('%Y-%m-%d')
today_count = CsdnCommentsManager.get_comments_count(username, today_date)
yesterday_date = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
yesterday_count = CsdnCommentsManager.get_comments_count(username, yesterday_date)
if today_count == -1 or yesterday_count == -1:
messagebox.showerror("Error", "请求出错或解析出错")
else:
output_text.set(f"{username} 今天的评论数量为: {today_count}\n"
f"{username} 昨天的评论数量为: {yesterday_count}\n"
f"{username} 最近2天的评论总数量为: {yesterday_count + today_count}")
history = CsdnCommentsManager.load_history()
CsdnCommentsManager.update_history(history, username)
combobox['values'] = history
def center_window(root, width, height):
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(size)
# 创建主窗口
root = tk.Tk()
root.title("CSDN 评论计数器")
root.geometry("400x400")
center_window(root, 400, 400) # 调整窗口大小以适应你的界面布局
# 创建提示标签
label = tk.Label(root, text="在此输入账号:")
label.pack(pady=10)
# 创建下拉菜单
history = CsdnCommentsManager.load_history()
combobox = ttk.Combobox(root, values=history)
if len(history) != 0:
combobox.current(0)
combobox.pack(pady=10)
# 创建按钮
button = tk.Button(root, text="查询评论数量", command=lambda: fetch_comments(combobox))
button.pack(pady=10)
# 创建输出区域
output_text = tk.StringVar()
output_label = tk.Label(root, textvariable=output_text, justify="left")
output_label.pack(pady=10)
# 运行主循环
root.mainloop()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。