程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

A translation tool written by python3 + wxPython

編輯:Python

Python3 + wxpython Write a translation tool


There has always been a problem in learning Japanese , Sometimes we see a sentence , I can probably guess what it means , But Japanese Kanji doesn't know how to read . I have such trouble , It has greatly reduced my enthusiasm for learning Japanese .
So these two days in the company's spare time , I just want to use the Internet api Do a Japanese Kanji translation kana a small tool .

At first I want to use Baidu translation api, Because Baidu translation has a button to display Japanese kana to view . But it seems very simple api, But I don't know why I can't get the translation results . Go back and use Baidu to translate the default parameters , But you can't get it by changing a keyword .

Actually, it's a success , It doesn't meet my requirements . Because Baidu translation should be able to display pseudonyms , It must be translated into Japanese by some language , You cannot select Japanese at the same time , From the bottom of my heart , I don't want to spend any more time studying .

So I began to think about using python Reptiles to achieve , Proper translation , Google Translate , There is no function to display pseudonyms . Heaven never shuts one door but opens another , I saw the Jianghu small D Word translation , The kana appears just below .

Open text translation , Although there is no function to display Kanas , But the background has the data I want , You can also choose from Japanese to Japanese , This really makes me overjoyed . Don't say the , starts !

One 、 Analysis request

python The most important thing for a crawler is the analysis and construction of requests , And just Hujiang is small D It's not that much trouble here . Just write with one content Parameters , Plus the request header cookie You can get the data . The translation direction is spliced in the access path .

for example :https://dict.hjenglish.com/v10/dict/translation/jp/jp It means translating from Japanese to Japanese .

Be careful :cookie Be sure to have , Otherwise, you will get an abnormal access message .
The code to get the response result is posted here :

import requests
base_url = "https://dict.hjenglish.com/v10/dict/translation"
# If you need to set up an agent in the company , Not for personal use proxies Parameters 
proxies = {

'http': SETTINGS['http_proxy'],
'https': SETTINGS['https_proxy']
}
headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3756.400 QQBrowser/10.5.4043.400',
'referer': 'https://dict.hjenglish.com/app/trans',
'cookie': 'HJ_UID=62ecdcb4-18dd-b89a-39b8-d3ae29f7a945; TRACKSITEMAP=3%2C20%2C23; _SREG_20=www.baidu.com%7C%7Csearch%7Cdomain; _SREF_20=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DhyKgBOkGFQR01SSRmo7VknK4QwVQIldqklx5Rp8DnEZEuuKZKZLW6HA_szdxSEOUebC5lDawvY00P_wQbBhKcMfyRi1L_8i4NtSXXbpSHCkFAVUmErnF8jeIacjvCaWTF9a0YnL9vR3XJSErsWNjIa%26wd%3D%26eqid%3Dd56c85360010ae6b00000003603f21b6; _SREF_3=; _REF=; acw_tc=707c9fc316147644229968942e605090718ce7e184164b887c24be999348a4; HJ_CST=0; HJ_CSST_3=0; HJ_SID=4pdfs7-0aa1-46a9-a81d-52e432e2efd8; HJ_SSID_3=4pdfs7-51bb-42aa-824c-ffa74e82386d; _SREG_3=direct%7C%7Cdirect%7Cdirect; _REG=direct%7C%7Cdirect%7Cdirect'
}
session = requests.Session()
url = f"{base_url}/cn/jp"
response = ""
try:
response = session.post(url, data={

"content": keyword
}, proxies=proxies, headers=headers)
except:
print(' Network connection error !')
return None
if response.status_code != 200:
trans_result['trans_text'] = 'error!'
return None
res_dict = response.json()['data']
print(res_dict)


Got the data , Everyone can play freely . Let me show you my achievements .

The interface is very simple , It realizes the translation between different languages , In Japanese, the results with and without Kanas will be displayed on the right . The following is a historical translation record , Will be saved locally , Each time you open the application, it loads automatically . When the voice reading is available , The translation results will be read out automatically .

The above is a record of this study , Welcome to leave a message to discuss . Program packaged , Please download by yourself :https://download.csdn.net/download/qq_36991535/15561961, If you need the source code, please send me a private letter .

The development of this gadget , Only for learning . If there is any infringement , Please contact the QQ:1593324992, I will delete it in time .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved