1681280457930.png (138.58 KB, 下载次数: 0)
下载附件
2023-4-12 14:22 上传
[Python] 纯文本查看 复制代码import sys
import requests
import os
from PySide2 import QtWidgets, QtGui, QtCore
from PySide2.QtCore import Qt
from bs4 import BeautifulSoup
class HanziSearch(QtWidgets.QWidget):
def __init__(self):
super(HanziSearch, self).__init__()
self.setWindowTitle('静待花开久不开')
self.layout_up = QtWidgets.QHBoxLayout()
self.layout_down = QtWidgets.QHBoxLayout()
self.edit_find = QtWidgets.QLineEdit()
self.edit_find.setFixedHeight(50)
self.edit_find.setPlaceholderText("请输入一个汉字")
self.btn_search = QtWidgets.QPushButton("查询")
self.layout_up.addWidget(self.edit_find)
self.layout_up.addWidget(self.btn_search)
self.lbl_gif = QtWidgets.QLabel()
self.movie = QtGui.QMovie('./空.gif')
self.lbl_gif.setMovie(self.movie)
self.movie.start()
self.lbl_gif.setWindowFlags(Qt.SplashScreen)
self.layout_right = QtWidgets.QVBoxLayout()
self.edit_pinyin = QtWidgets.QLineEdit()
self.edit_pinyin.setFixedHeight(50)
self.edit_pinyin.setPlaceholderText("拼音")
self.edit_radical = QtWidgets.QLineEdit()
self.edit_radical.setFixedHeight(50)
self.edit_radical.setPlaceholderText("部首")
self.edit_stroke = QtWidgets.QLineEdit()
self.edit_stroke.setFixedHeight(50)
self.edit_stroke.setPlaceholderText("笔画")
self.edit_traditional = QtWidgets.QLineEdit()
self.edit_traditional.setFixedHeight(50)
self.edit_traditional.setPlaceholderText("繁体")
self.edit_wubi = QtWidgets.QLineEdit()
self.edit_wubi.setFixedHeight(50)
self.edit_wubi.setPlaceholderText("五笔")
self.layout_right.addWidget(self.edit_pinyin)
self.layout_right.addWidget(self.edit_radical)
self.layout_right.addWidget(self.edit_stroke)
self.layout_right.addWidget(self.edit_traditional)
self.layout_right.addWidget(self.edit_wubi)
self.layout_down.addWidget(self.lbl_gif)
self.layout_down.addLayout(self.layout_right)
self.setLayout(QtWidgets.QVBoxLayout())
self.layout().addLayout(self.layout_up)
self.layout().addLayout(self.layout_down)
self.btn_search.clicked.connect(self.search_hanzi)
#界面美化
self.edit_find.setFont(QtGui.QFont("微软雅黑", 18))
self.btn_search.setFont(QtGui.QFont("微软雅黑", 18))
self.btn_search.setStyleSheet("background-color:#37a7e8; color:#fff")
self.edit_pinyin.setFont(QtGui.QFont("微软雅黑", 18))
self.edit_radical.setFont(QtGui.QFont("微软雅黑", 18))
self.edit_stroke.setFont(QtGui.QFont("微软雅黑", 18))
self.edit_traditional.setFont(QtGui.QFont("微软雅黑", 18))
self.edit_wubi.setFont(QtGui.QFont("微软雅黑", 18))
self.layout_up.setSpacing(20)
self.layout_down.setSpacing(60)
self.layout_right.setSpacing(18)
self.layout_right.setContentsMargins(0, 0, 0, 0)
self.setStyleSheet(
'''
QWidget{background-color:#fff;}
QGridLayout::item{margin:5px;}
QLineEdit{border:1px solid #d3d3d3;border-radius: 5px;height:24px;}
QLineEdit:hover{border:1px solid #979797;}
QPushButton{border:1px solid #37a7e8;border-radius:2px;color:#fff;padding: 4px 16px;background:#37a7e8;}
QPushButton:hover{background-color:#4fbcff;}
'''
)
def search_hanzi(self):
self.btn_search.setText("查询中...")
self.btn_search.setEnabled(False)
hanzi = self.edit_find.text()
if not self.is_chinese(hanzi) or hanzi=='' or len(hanzi)>1:
QtWidgets.QMessageBox.warning(self, "提示", "请输入一个汉字!")
self.btn_search.setText("查询")
self.btn_search.setEnabled(True)
else:
self.movie.start()
self.finder(hanzi)
def finder(self, hanzi):
self.movie.stop()
filename = f"{hanzi}.gif"
url = f"https://hanyu.baidu.com/zici/s?wd={hanzi}"
html = requests.get(url)
soup = BeautifulSoup(html.text, features="html.parser")
if os.path.isfile(filename):
self.movie.setFileName(filename)
self.movie.start()
else:
img = soup.findAll("img", {"class": "bishun"})[0].get("data-gif")
res = requests.get(img)
with open(filename,"wb") as f:
f.write(res.content)
self.movie.setFileName(filename)
self.movie.start()
#拼音
pinyin = soup.findAll("div", {"id": "pinyin"})[0].find_all("b")[0].string
self.edit_pinyin.setText('拼音:' + pinyin)
#部首
radical = soup.findAll("li", {"id": "radical"})[0].find_all("span")[0].string
self.edit_radical.setText('部首:' + radical)
#笔数
stroke = soup.findAll("li", {"id": "stroke_count"})[0].find_all("span")[0].string
self.edit_stroke.setText('笔数:' + stroke)
#繁体
traditional = soup.findAll("li", {"id": "traditional"})[0].find_all("span")[0].string
self.edit_traditional.setText('繁体:' + traditional)
#五笔
wubi = soup.findAll("li", {"id": "wubi"})[0].find_all("span")[0].string
self.edit_wubi.setText('五笔:' + wubi)
self.btn_search.setText("查询")
self.btn_search.setEnabled(True)
def is_chinese(self, s):
for c in s:
if not ('\u4e00'