吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2049|回复: 15
收起左侧

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

  [复制链接]
cdsgg 发表于 2020-12-12 18:19
本帖最后由 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
GIF 2020-12-12 18-00-35.gif

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
cz54188 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| cdsgg 发表于 2020-12-12 18:34
你们可以改一下104行 改成
[Python] 纯文本查看 复制代码
	def inJudgment(self):
		if self.textEdit.toPlainText()=='':
			self.translationEnglish()
		elif self.textEdit_2=='':
			self.StartTranslat()
		else:
			self.StartTranslat()
 楼主| cdsgg 发表于 2020-12-12 18:45
[Python] 纯文本查看 复制代码
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 ,也不算是api吧
空想昼夜 发表于 2020-12-12 19:53
这调搜狗不算api嘛
 楼主| cdsgg 发表于 2020-12-12 20:02

那算也算吧
vtvhul2y 发表于 2020-12-12 20:08
又叫文章去重工具
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 21:48

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表