增强模拟人操作的效果,并且确保只有在百度百科和搜狗百科都正确输出时才保存记录,我们在数据集搜集器(百科)015代码中增加一些逻辑来实现这一点。以下是改进后的代码:
import tkinter as tk
from tkinter import filedialog, messagebox
import requests
from bs4 import BeautifulSoup
import json
import os
import threading
from tkinter import ttk
import logging
import time
import random # 导入随机模块
# 配置日志
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 读取配置文件
CONFIG_FILE = 'config.json'
DEFAULT_CONFIG = {
"record_folder": "记录",
"log_file": "app.log"
}
def load_config():
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'r', encoding='utf-8') as file:
return json.load(file)
return DEFAULT_CONFIG
config = load_config()
class BaikeSearchApp:
def __init__(self, root):
self.root = root
self.root.title("百度百科查询工具")
# 创建输入框
self.input_frame = tk.Frame(root)
self.input_frame.pack(pady=5)
self.input_label = tk.Label(self.input_frame, text="输入问题:")
self.input_label.pack(side=tk.LEFT, padx=5)
self.input_entry = tk.Entry(self.input_frame, width=60)
self.input_entry.pack(side=tk.LEFT, padx=5)
self.clear_input_button = tk.Button(self.input_frame, text="清除", command=self.clear_input)
self.clear_input_button.pack(side=tk.LEFT, padx=5)
# 创建文本框
self.text_frame = tk.Frame(root)
self.text_frame.pack(pady=10)
self.text = tk.Text(self.text_frame, wrap='word', height=20, width=80)
self.text.pack(pady=10)
# 创建按钮
self.button_frame = tk.Frame(root)
self.button_frame.pack(pady=5)
self.load_button = tk.Button(self.button_frame, text="加载文件", command=self.load_file
标签:百科,self,搜集,text,tk,016,import,input,frame
From: https://blog.csdn.net/weixin_54366286/article/details/144292608