Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the artifacts of Python translation?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you what Python translation artifacts are available, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Text

Do you often encounter these problems: look down! Today, I will teach you to write one of the three translation artifacts. You deserve it!

Environment installation: python3.6, pycharm2021, and built-in modules.

Pip install-I https://pypi.douban.com/simple/ requests pip install-I https://pypi.douban.com/simple/ pyqt5

A simple interface can be optimized.

Class Translator (QWidget): def _ _ init__ (self, parent=None, * * kwargs): super (Translator Self). _ init__ (parent) self.setWindowTitle ('three translation software') self.setWindowIcon (QIcon ('data/icon.jpg')) self.Label1 = QLabel (' original') self.Label2 = QLabel ('translated') self.LineEdit1 = QLineEdit () self.LineEdit2 = QLineEdit () self.translateButton1 = QPushButton () self.translateButton2 = QPushButton () self.translateButton3 = QPushButton () self.translateButton1.setText ('Baidu Translation') self.translateButton2.setText ('Youdao Translation') self.translateButton3.setText ('Google Translation') self .grid = QGridLayout () self.grid.setSpacing (12) self.grid.addWidget (self.Label1 1,0) self.grid.addWidget (self.LineEdit1, 1,1) self.grid.addWidget (self.Label2, 2,0) self.grid.addWidget (self.LineEdit2, 2,1) self.grid.addWidget (self.translateButton1, 1,2) self.grid.addWidget (self.translateButton2, 2,2) self.grid.addWidget (self.translateButton3 3, 2) self.setLayout (self.grid) self.resize Self.translateButton1.clicked.connect (lambda: self.translate (api='baidu')) self.translateButton2.clicked.connect (lambda: self.translate (api='youdao')) self.translateButton3.clicked.connect (lambda: self.translate (api='google')) self.bd_translate = baidu () self.yd_translate = youdao () Self.gg_translate = google () def translate (self Api='baidu'): word = self.LineEdit1.text () if not word: return if api= = 'baidu': results = self.bd_translate.translate (word) elif api= =' youdao': results = self.yd_translate .translate (word) elif api = 'google': results = self.gg_translate.translate (word) else: raise RuntimeError (' Api should be or or...') Self.LineEdit2.setText ('; '.join (results))

One of the three major translators: Baidu translation category.

Class baidu (): def _ _ init__ (self): self.session = requests.Session () self.session.cookies.set ('BAIDUID',' 19288887A2239549097302637D1DEBPLV FGene1X') self.session.cookies.set ('PSTM','% d '% int (time.time ()) self.headers = {' User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64) X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'} self.data = {'from':', 'to':'' 'query':', 'transtype':' translang', 'simple_means_flag':' 3' 'sign':', 'token':' 'domain':' common'} self.url = 'https://fanyi.baidu.com/v2transapi' self.langdetect_url =' https://fanyi.baidu.com/langdetect' def translate (self Word): self.data ['from'] = self.detectLanguage (word) self.data [' to'] = 'en' if self.data [' from'] = = 'zh' else' zh' self.data ['query'] = word self.data [' token'] Gtk = self.getTokenGtk () self.data ['token'] =' 6482f137ca44f07742b2677f5ffd39e1' self.data ['sign'] = self.getSign (gtk, word) res = self.session.post (self.url) Data=self.data) return [res.json () ['trans_result'] [' data'] [0] ['result'] [0] [1]] def getTokenGtk (self): url =' https://fanyi.baidu.com/' res = requests.get (url, headers=self.headers) token = re.findall (r "token:'(. *?)'" Res.text) [0] gtk = re.findall (r " Window.gtk = ('. *?'); ", res.text) [0] return token, gtk def getSign (self, gtk, word): evaljs = js2py.EvalJs () js_code = js.bd_js_code js_code = js_code.replace ('null! = = I? Word): data= {'query': word} res = self.session.post (self.langdetect_url, headers=self.headers, data=data) return res.json () [' lan']

The second of the three major translators: Youdao Translation.

Class youdao (): def _ _ init__ (self): self.headers = {'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64) X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36', 'Referer':' http://fanyi.youdao.com/', 'Cookie':' OUTFOX_SEARCH_USER_ID=-481680322@10.169.0.83 '} self.data = {' ibis: None, 'from':' AUTO', 'to':' AUTO' 'smartresult': 'dict',' client': 'fanyideskweb',' salt': None, 'sign': None 'ts': None, 'bv': None,' doctype': 'json',' version': '2.1' 'keyfrom':' fanyi.web', 'action':' FY_BY_REALTlME'} self.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' def translate (self) Word): ts = str (int (time.time () * 10000)) salt = ts + str (int (random.random () * 10)) sign = 'fanyideskweb' + word + salt +' 97y3 (jkMYg@T [KZQmqjTK' sign = hashlib.md5 (sign.encode ('utf-8')). Hexdigest () bv =' 5.0 (Windows NT 10.0) Win64 X64) AppleWebKit/537.36 (KHTML Like Gecko) Chrome/70.0.3538.77 Safari/537.36' bv = hashlib.md5 (bv.encode ('utf-8')). Hexdigest () self.data [' i'] = word self.data ['salt'] = salt self.data [' sign'] = sign self.data ['ts'] = ts Self.data ['bv'] = bv res = requests.post (self.url Headers=self.headers, data=self.data) return [res.json () ['translateResult'] [0] [0] .get (' tgt')]

The third of the three major translations: Google translation.

Class google (): def _ _ init__ (self): self.headers = {'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64) X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',} self.url = 'https://translate.google.cn/translate_a/single?client=t&sl=auto&tl={}&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&tk={}&q={}' def translate (self) Word): if len (word) > 4891: raise RuntimeError ('The length of word should be less than 4891..') Languages = ['zh-CN',' en'] if not self.isChinese (word): target_language = languages [0] else: target_language = languages [1] res = requests.get (target_language, self.getTk (word), word) Headers=self.headers) return [res.json () [0] [0] [0]] def getTk (self, word): evaljs = js2py.EvalJs () js_code = js.gg_js_code evaljs.execute (js_code) tk = evaljs.TL (word) return tk def isChinese (self Word): for w in word: if'\ u4e00'

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report