cdsgg 发表于 2020-12-12 18:19

调用api做一个中英互翻小工具带图形界面(没啥技术含量)

本帖最后由 cdsgg 于 2020-12-12 20:02 编辑

# -*- 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.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.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

cdsgg 发表于 2020-12-12 18:34

你们可以改一下104行 改成        def inJudgment(self):
                if self.textEdit.toPlainText()=='':
                        self.translationEnglish()
                elif self.textEdit_2=='':
                        self.StartTranslat()
                else:
                        self.StartTranslat()

cdsgg 发表于 2020-12-12 18:45

url=f'https://fanyi.sogou.com/?keyword={english}&transfrom=en&transto=zh-CHS&model=general'
70行代码中的transfrom=en改改jp就能翻译日语同理可得

rossi2020 发表于 2020-12-12 18:28

来学习~谢谢楼主

ma4907758 发表于 2020-12-12 18:36

不错,学习了,感谢

帅瑞瑞 发表于 2020-12-12 19:14

这不是调得搜狗的接口api么

cdsgg 发表于 2020-12-12 19:21

帅瑞瑞 发表于 2020-12-12 19:14
这不是调得搜狗的接口api么

这直接改网址的 不算是调用api {:1_905:},也不算是api吧

空想昼夜 发表于 2020-12-12 19:53

这调搜狗不算api嘛{:301_983:}

cdsgg 发表于 2020-12-12 20:02

空想昼夜 发表于 2020-12-12 19:53
这调搜狗不算api嘛

:lol那算也算吧

vtvhul2y 发表于 2020-12-12 20:08

又叫文章去重工具{:1_900:}
页: [1] 2
查看完整版本: 调用api做一个中英互翻小工具带图形界面(没啥技术含量)