50 lines of Python code, easy to translate Chinese and English

Python learning exchange group: 125240963, daily sharing of dry goods within the group, including the latest Python enterprise case study materials and zero basic introductory course, welcome to join the group to learn and exchange

Nowadays, more and more attention is paid to English ability. If you know English, you will come into contact with a larger world, and you will find more information. Especially for programmers, good programming materials come from English documents. Many of the translated versions in China are so different from the original ones that they are even wrong.

Although there are a lot of translation software now, it's a great sense of achievement to write a small python program by yourself. Even with today's code, you can develop your own small translation software.

Tao Dictionary interface

Today's translation program is implemented by using the interface of Dao translation. The logic of the program is very simple, that is to use the Daoist translation, take the content to be translated as a parameter, and send it to the corresponding url. Then we can get the corresponding translation results by returning a json data through the server with Tao.

Program call results

Start the program in the terminal input you want to translate the content, can be translated into English, can also be translated into English. For example, type "Koc is the most handsome! "


Corresponding input English can also be translated into Chinese.


Program code

The program code is very simple. I have comments in the corresponding code.

 1 import json
 3 import requests
 5 # Translation function, word Content to be translated
 6 def translate(word):
 7     # Youdao Dictionary api
 8     url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
 9     # Parameters transferred, where i For content to be translated
10     key = {
11         'type': "AUTO",
12         'i': word,
13         "doctype": "json",
14         "version": "2.1",
15         "keyfrom": "fanyi.web",
16         "ue": "UTF-8",
17         "action": "FY_BY_CLICKBUTTON",
18         "typoResult": "true"
19     }
20     # key This dictionary is the content sent to the Youdao dictionary server
21     response = requests.post(url, data=key)
22     # Judge whether the server is successful
23     if response.status_code == 200:
24         # And then the corresponding results
25         return response.text
26     else:
27         print("Failed to call Youdao dictionary")
28         # Corresponding failure returns null
29         return None
31 def get_reuslt(repsonse):
32     # adopt json.loads Load the returned result as json format
33     result = json.loads(repsonse)
34     print ("The words entered are:%s" % result['translateResult'][0][0]['src'])
35     print ("The translation results are as follows:%s" % result['translateResult'][0][0]['tgt'])
37 def main():
38     print("This program calls the API Translation can achieve the following results:")
39     print("foreign language-->Chinese")
40     print("Chinese-->English")
41     word = input('Please input the words or sentences you want to translate:')
42     list_trans = translate(word)
43     get_reuslt(list_trans)
45 if __name__ == '__main__':
46     main()


Author: Chi Hai
Link: https://juejin.im/post/5b4bdb81e51d45616f4575dd

Tags: Python JSON Programming

Posted on Mon, 03 Feb 2020 07:35:46 -0800 by Gast