自動翻譯器1

image.png

自動翻譯器的python部分

一、設計思路

1.qt提取剪貼板/鼠標選中內容作爲翻譯內容

2.使用python向百度翻譯提交翻譯內容,然後取回翻譯結果

3.使用qt顯示翻譯結果

二、實現步驟

我們已經安裝了Jupyter作爲開發環境

  1. 先分析百度翻譯提交接口

有三種接口方式可以使用

  • 地址欄https://fanyi.baidu.com/translate?aldtype=16047&query=&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/world world就是要查詢的單詞
  • https://fanyi.baidu.com/sug這個是百度自動識別的單詞下拉項
    1564905012628.png
  • https://fanyi.baidu.com/v2transapi這個是真正百度翻譯的接口

這三種接口各有優缺點:

  • 第一種url簡單,使用方便,缺點就是返回的是一整個網頁,需要從網頁中提取翻譯的內容,但是該網頁是動態渲染的,裏面並沒有我們需要的信息
  • 第二種並不是一個真實的翻譯,只是百度檢索出類似的情況,不一定是需要的,而且如果是一句話的翻譯,這個是空的
  • 第三種是真正的翻譯,但是需要提交詳細數據,下面就是要提交的數據,其他數據還好,這個sign比較麻煩,他是js動態生成的,是加密的,我們無法模擬
from: en
to: zh
query: world
transtype: realtime
simple_means_flag: 3
sign: 335290.130699
token: fcd815f24ac02a1ddc7c485f38c8efe8

綜合考慮,這三種我們都要放棄。

針對動態渲染的網頁,Python提供了許多模擬瀏覽器運行的庫,比如Selenium

  1. 使用Selenium

首先命令行安裝selenium

pip install selenium -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
  1. 導入模塊
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  1. 封裝類
class Translation:
    def __init__(self):
        self.options = webdriver.ChromeOptions()   
        self.options.add_argument('headless')    # 後臺運行        
#         self.options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
        # 禁止圖片的加載
        self.prefs = {"profile.managed_default_content_settings.images":2}
        self.options.add_experimental_option("prefs",self.prefs)
        self.browser = webdriver.Chrome(executable_path='chromedriver.exe', options=self.options)#, desired_capabilities=self.desired_capabilities)

        self.load = False
        
    def __del__(self):
        self.browser.close()
        
    def translate(self, words):
        try:
            if not self.load :
                self.load = True
                self.browser.get('https://fanyi.baidu.com/translate#en/zh/')
            self.input= self.browser.find_element_by_id('baidu_translate_input')
            self.input.clear()
            self.input.send_keys(words)
            
            self.button = self.browser.find_element_by_id('translate-button')
            self.button.click()

            time.sleep(1)

            self.out = self.browser.find_element_by_class_name('output-bd')
            print (self.out.text)
            return self.out.text

        finally:
#             print ("translate [ {0} ] error.".format(words))
            return ''
        
  1. 測試類
if __name__ == '__main__':
    words = [
        '0',
        'Taylor was nominated for a Primetime Emmy Award last year for portraying Minnie in the latest Mickey Mouse TV show rendition ',
        '"I really want whoever comes after us to be aware of the history and the tradition, and to love the characters as much as we do," Taylor said about herself and Allwine, according to Disney.',
        '"Minnie Mouse lost her voice with the passing of Russi Taylor," Bob Iger, Disney Chairman and CEO, wrote on Twitter.'
    ]

    print ('---start-')
    translation = Translation()
    for w in words:
        print ('----', w)
        result = translation.translate(w)
        print (result)
    
    del translation
    
  1. 測試結果
---start-
---- 0

---- Taylor was nominated for a Primetime Emmy Award last year for portraying Minnie in the latest Mickey Mouse TV show rendition 
泰勒去年因在最新的米奇老鼠電視節目“表演”中飾演米妮而獲得艾美獎的提名。

---- "I really want whoever comes after us to be aware of the history and the tradition, and to love the characters as much as we do," Taylor said about herself and Allwine, according to Disney.
“我真的希望任何一個追隨我們的人都能意識到歷史和傳統,並且像我們一樣熱愛這些角色,”根據迪士尼的說法,泰勒在談到自己和奧爾溫時說。

---- "Minnie Mouse lost her voice with the passing of Russi Taylor," Bob Iger, Disney Chairman and CEO, wrote on Twitter.
迪斯尼董事長兼首席執行官鮑勃•伊格爾在Twitter上寫道:“米妮•老鼠在路西•泰勒去世後失去了聲音。”

之所以第一個數據是0,是因爲未知原因第一個翻譯時候,網頁會刷新,導致得不到翻譯結果,所以需要屏蔽。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章