本帖最后由 cdsgg 于 2020-12-12 20:02 编辑
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '翻译着玩.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication,QMessageBox,QMainWindow
import sys
import requests
from bs4 import BeautifulSoup
import re
import time
from urllib.request import quote
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(323, 203)
Form.setWindowIcon(QIcon('translation.ico'))
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(0, 60, 321, 51))
self.textEdit.setObjectName("textEdit")
self.textEdit_2 = QtWidgets.QTextEdit(Form)
self.textEdit_2.setGeometry(QtCore.QRect(0, 150, 321, 51))
self.textEdit_2.setObjectName("textEdit_2")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(60, 120, 171, 16))
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(20, 10, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(160, 10, 91, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton.clicked.connect(self.inJudgment)
self.pushButton_2.clicked.connect(self.emptyContent)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "翻译着玩"))
self.textEdit.setPlaceholderText(_translate("Form", "请在这里输入中文"))
self.textEdit_2.setPlaceholderText(_translate("Form", "请在这里输入英文"))
self.label.setText(_translate("Form", "翻译结果"))
self.pushButton.setText(_translate("Form", "翻译"))
self.pushButton_2.setText(_translate("Form", "清空翻译内容"))
def StartTranslat(self):
# toPlainText()
# 获取文本
chinese=self.textEdit.toPlainText()
english=self.textEdit_2.toPlainText()
CN =quote(chinese)
# EN=quote(english)
# for i in range(70):
url = f'https://fanyi.sogou.com/?keyword={CN}&transfrom=zh-CHS&transto=en&model=general'
headers = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'}
req = requests.get(url=url, headers=headers).content.decode()
soup = BeautifulSoup(req, 'lxml')
res = soup.select(
'#J-container > div.content-wrap > div > div:nth-child(3) > div.trans-box > div.trans-to.trans-to-border > div.output > p')
self.textEdit_2.setText(res[0].text)
def emptyContent(self):
self.textEdit.setText('')
self.textEdit_2.setText('')
def translationEnglish(self):
#https://fanyi.sogou.com/?keyword=hello&transfrom=en&transto=zh-CHS&model=general
english=self.textEdit_2.toPlainText()
url=f'https://fanyi.sogou.com/?keyword={english}&transfrom=en&transto=zh-CHS&model=general'
headers = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'}
req = requests.get(url=url, headers=headers).content.decode()
soup = BeautifulSoup(req, 'lxml')
res = soup.select(
'#J-container > div.content-wrap > div > div:nth-child(3) > div.trans-box > div.trans-to.trans-to-border > div.output > p')
self.textEdit.setText(res[0].text)
def inJudgment(self):
if self.textEdit.toPlainText()=='':
self.translationEnglish()
else:
self.StartTranslat()
if __name__=="__main__":
app=QApplication(sys.argv)
w=QMainWindow()
UI=Ui_Form()
UI.setupUi(w)
w.show()
app.exec_()
成品链接:
https://wwa.lanzouj.com/iNQJQjarn5e
|